pratamishus Posted November 22, 2009 Share Posted November 22, 2009 Hello people! My problem is as follows: I need to convert a partially transparent PNG to a transparent GIF. Everything works fine except of the background problem. You might know such thing in photoshop as "Matte" for the GIF images. So when I convert my PNG to a GIF I want to set the Matte of the image (for instance #00FF00). This means that I want to set a green background to the PNG image and then set all the parts that are absolutely equal to 00FF00 (0, 255, 0) to become transparent. Also I can't create loops running through each pixel as this loads my server with the amount of requests I have. Here is the part of the code that creates my backround: imagealphablending($image_resized, true); // Create a new transparent color for image $color = imagecolorallocatealpha($image_resized,$back_color[0], $back_color[1], $back_color[2], 127); // Completely fill the background of the new image with allocated color. $color=imagecolorallocate($image_resized, $back_color[0], $back_color[1], $back_color[2]); imagefill($image_resized, 0, 0, $color); // Restore transparency blending imagesavealpha($image_resized, true); //$black = imagecolorallocate($image_resized, $back_color[0], $back_color[1], $back_color[2]); // Make the background transparent imagecolortransparent($image_resized, $color); In the given code: if I remove the "imagefill" line the script works, but the Matte becomes black. If I enable it, background sets up perfectly, but the transparency works wrong. Can anyone tell me what is wrong? Link to comment https://forums.phpfreaks.com/topic/182499-png-to-gif/ Share on other sites More sharing options...
Cardale Posted November 22, 2009 Share Posted November 22, 2009 function png2gif($pngs, $background = array(255, 255, 255), $dest = 'gif'){ foreach($pngs as $png){ $size = getimagesize($png); $img = imagecreatefrompng($png); $image = imagecreatetruecolor($width = $size[0], $height = $size[1]); imagefill($image, 0, 0, $bgcolor = imagecolorallocate($image, $background[0], $background[1], $background[2])); imagecopyresampled($image, $img, 0, 0, 0, 0, $width, $height, $width, $height); imagecolortransparent($image, $bgcolor); imagegif($image, str_ireplace('.png', '.gif', $dest.DIRECTORY_SEPARATOR.basename($png)), 100); imagedestroy($image); } } // example png2gif(glob("icons/*.png")); Sorry if this doesn't help didn't spend much time on it. Link to comment https://forums.phpfreaks.com/topic/182499-png-to-gif/#findComment-963318 Share on other sites More sharing options...
pratamishus Posted November 23, 2009 Author Share Posted November 23, 2009 Hi! Thank you for the reply, but for some reason - it did not work. I know it should be and I have no idea why it isn't working. I have done it in the most stupid way possible, but working: for($z=0; $z<$final_width; $z++) { for($y=0; $y<$final_height; $y++) { $rgb=imagecolorat($image_resized, $z, $y); if($rgb) imagesetpixel($image_resized, $z, $y, $bcolor); } } If anyone can suggest a better way, I would thank that person everyday I live Link to comment https://forums.phpfreaks.com/topic/182499-png-to-gif/#findComment-963824 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.