Page 1 of 1

imagecreatefromgif & imagecolortransparent() ??

Posted: Tue Dec 02, 2008 12:59 pm
by lukwe
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi all, the simple snippet

Code: Select all

 
header('Content-type: image/gif');
$im = imagecreatefromgif("/path/Mypic.gif");
$background = imagecolorallocate($im, 255, 255, 255);
imagecolortransparent($im,$background);
 
imagegif($im,'/path/TransparentPic.gif');
imagedestroy($im);
 
is creating the new image but is not setting the white background to transparent; a similarly simple test based on imagecreatetruecolor() works correctly... any hints?!

Ciao, Luca


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: imagecreatefromgif & imagecolortransparent() ??

Posted: Wed Dec 03, 2008 5:42 am
by lukwe
ok, sorry for missing the

Code: Select all

syntax...

Re: imagecreatefromgif & imagecolortransparent() ??

Posted: Wed Dec 03, 2008 7:29 am
by onion2k
If the file mypic.gif already contains 256 different colors then the call to imagecolorallocate() will fail (silently I think). Also, you're assuming that 255,255,255 is actually in the image. It might not be. A pixel of 254,255,255 will look white, but won't match... Try imagecolorexact() or one of the similar functions.

Re: imagecreatefromgif & imagecolortransparent() ??

Posted: Wed Dec 03, 2008 9:58 am
by lukwe
>If the file mypic.gif already contains 256 different colors then the call to imagecolorallocate() will fail (silently I think).

mypic.gif contains 4 colors only...

>you're assuming that 255,255,255 is actually in the image. It might not be.

I am sure about this, both because I created the images from a script, and because I verified it with
imagecolorat() + imagecolorsforindex() on various (x,y)

I tried adding 'imagecolordeallocate($im, $white);' and re-allocating it with imagecolorallocatealpha($im, 255, 255, 255, 127);
Still fails, being dependent on the initial imagecolorallocate() to set $im.

Hence I tried a different method:

Code: Select all

 
$src = imagecreatefromgif("original.gif");
$dest = imagecreatetruecolor(190, 190);
 
//
$white = imagecolorallocate($dest, 255, 255, 255);
imagecolortransparent($dest, $white);
 
//copy 
imagecopy($dest, $src, 0, 0, 0, 0, 190, 190);
 
echo "done!";
 
// Output and free from memory
header('Content-Type: image/gif');
imagegif($dest,'copiedOne.gif');
 
imagedestroy($dest);
imagedestroy($src);
 
This also fails with all permutations of imagecreate()/imagecreatetruecolor(), imagecolorallocatealpha(); etc

Re: imagecreatefromgif & imagecolortransparent() ??

Posted: Thu Dec 04, 2008 6:15 am
by lukwe
this works:

Code: Select all

<?php
$im = imagecreatefromgif("beachball1.gif");
$white = imagecolorexact($im, 255, 255, 255);
imagecolortransparent($im, $white);
imagegif($im, 'newBeachB.gif');
imagedestroy($im);
?>
so basically stay away from imagecolorallocate(), which references the color palette in the original image.

Thanks to filippo.toso on the php forum on HTML.it for the above snippet.