imagecreatefromgif & imagecolortransparent() ??

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
lukwe
Forum Newbie
Posts: 4
Joined: Tue Dec 02, 2008 12:30 pm

imagecreatefromgif & imagecolortransparent() ??

Post 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.
lukwe
Forum Newbie
Posts: 4
Joined: Tue Dec 02, 2008 12:30 pm

Re: imagecreatefromgif & imagecolortransparent() ??

Post by lukwe »

ok, sorry for missing the

Code: Select all

syntax...
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: imagecreatefromgif & imagecolortransparent() ??

Post 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.
lukwe
Forum Newbie
Posts: 4
Joined: Tue Dec 02, 2008 12:30 pm

Re: imagecreatefromgif & imagecolortransparent() ??

Post 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
lukwe
Forum Newbie
Posts: 4
Joined: Tue Dec 02, 2008 12:30 pm

Re: imagecreatefromgif & imagecolortransparent() ??

Post 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.
Post Reply