a few words about web development

PHP looses transparency?

A simple step-by-step guide which will take less than 5 minutes
If your PHP script looses transparency when saving images in PHP format then most likely you forgot to add:
imagealphablending($img, false);
imagesavealpha($img, true);

But just to make thing more obvious here are 2 scripts to convert images from transparent GIF/PNG to transparent PNG.

Converting transparent PNG to transparent PNG


$img = imagecreatefromgif('image.gif');

//You can add some pixel transformations here

imagealphablending($img, false);
imagesavealpha($img, true);
imagepng($img, 'output.png');

Converting transparent GIF to transparent PNG


$img2 = imagecreatefromgif('image.gif');
list($wid, $hei) = array(imagesx($img2), imagesy($img2));

$img = imagecreatetruecolor($wid, $hei);
imagealphablending($img, false);
imagesavealpha($img, true);
$col = imagecolorallocatealpha($img, 0,0,0, 127);
imagefilledrectangle($img, 0,0, $wid, $hei, $col);
imagecopy($img, $img2, 0, 0, 0, 0, $wid, $hei);

//You can add some pixel transformations here

imagepng($img, 'output.png');

Comments