Last active
July 30, 2024 09:16
-
-
Save gullevek/f6aca570883707edc4a657a351481f5e to your computer and use it in GitHub Desktop.
PHP + GD 2.3.3 vs PHP + GD bundles (2.1.0 compatible) gif transparency problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Create transparent gif in certain sizes and print out base64 | |
$gd_version = gd_info()["GD Version"]; | |
// check: bundled -> use a transparency, if not and 2.3.3 or higher, use alternate | |
$use_imagecolorallocatealpha = true; | |
if (strstr($gd_version, "bundled") !== false) { | |
$use_imagecolorallocatealpha = false; | |
} | |
// demo sizes | |
$size_list = [ | |
[210, 296], | |
[220, 340], | |
[220, 440], | |
]; | |
$type_gif = 'gif'; | |
$type_png = 'png'; | |
$save_file = $_GET['save_file'] ?? 0; | |
$g_width = $_GET['width'] ?? 0; | |
$g_height = $_GET['height'] ?? 0; | |
if ( | |
is_numeric($g_width) && $g_width > 0 && $g_width < 1000 && | |
is_numeric($g_height) && $g_height > 0 && $g_height < 1000 | |
) { | |
$size_list = [ | |
[$g_width, $g_height] | |
]; | |
} else { | |
echo "Set Custom with _GET: width= and height=<br>"; | |
} | |
foreach ($size_list as $size) { | |
$width = $size[0]; | |
$height = $size[1]; | |
$im_gif = imagecreatetruecolor($width, $height); | |
$im_png = imagecreatetruecolor($width, $height); | |
// set always | |
imagesavealpha($im_gif, true); | |
imagealphablending($im_gif, true); | |
imagesavealpha($im_png, true); | |
imagealphablending($im_png, true); | |
// if bundled, use "old style" | |
if (!$use_imagecolorallocatealpha) { | |
// does not work: GIF: 2.3.3, works for GIF on bundled | |
$black_alpha_gif = imagecolorallocate($im_gif, 0, 0, 0); | |
$transparent_w = imagecolortransparent($im_gif, $black_alpha_gif); | |
} else { | |
// does work: GIF: 2.3.3 | |
$black_alpha_gif = imagecolorallocatealpha($im_gif, 0, 0, 0, 127); | |
} | |
// gif fill | |
imagefill($im_gif, 0, 0, $black_alpha_gif); | |
// PNG, same for both is ok | |
$black_alpha_png = imagecolorallocatealpha($im_png, 0, 0, 0, 127); | |
imagefill($im_png, 0, 0, $black_alpha_png); | |
ob_start(); | |
imagegif($im_gif); | |
$image_data_gif = ob_get_contents(); | |
ob_end_clean(); | |
ob_start(); | |
imagepng($im_png); | |
$image_data_png = ob_get_contents(); | |
ob_end_clean(); | |
// write | |
if ($save_file) { | |
imagegif($im_gif, 'images_output/' . $width . 'x' . $height . '.gif'); | |
imagepng($im_png, 'images_output/' . $width . 'x' . $height . '.png'); | |
} | |
imagedestroy($im_gif); | |
imagedestroy($im_png); | |
// output | |
echo "Width: <b>$width</b>, Height: <b>$height</b><br>"; | |
echo "<i>Base64 string GIF:</i><br>"; | |
echo '<div style="word-wrap: anywhere; font-family: monospace">'; | |
echo 'data:image/' . $type_gif . ';base64,' . base64_encode($image_data_gif); | |
echo "</div>"; | |
echo "<i>Base64 string PNG:</i><br>"; | |
echo '<div style="word-wrap: anywhere; font-family: monospace">'; | |
echo 'data:image/' . $type_png . ';base64,' . base64_encode($image_data_png); | |
echo "</div>"; | |
echo '<hr>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment