Page 1 of 1

problem generating thumbnail from a transparent gif

Posted: Wed Nov 29, 2006 1:48 am
by mr00047
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hello

i am trying to generate a thumbnail from a  transparent gif image. now that the problem is my whole image is get filled by the objects colors. i have studied the function provided in phpgd but it is not working.

please help urgent!

The code i am using to achieve this is:

Code: Select all

function imagecreatetruecolortransparent($x,$y) 
	{
        $i = imagecreatetruecolor($x,$y);
        //$b = imagecreatefromstring(base64_decode(blankpng()));//i have modified as i dont want any text to insert
        imagealphablending($i,false);
        imagesavealpha($i,true);
        //imagecopyresized($i,$b,0,0,0,0,$x,$y,imagesx($b),imagesy($b));
        return $i;
    }
function thumbnail_builder($src_image = "",$src_image_dir = "",$dst_image_dir ="",$new_image = "")
	{				
		
		if(exif_imagetype($src_image_dir.$src_image) == IMAGETYPE_GIF)
		{			
			
			$im = imagecreatefromgif($src_image_dir.$src_image) or die("cant read image");			
			
			$dst_im = $this->imagecreatetruecolortransparent(50,51);											

			imagecopyresampled($dst_im,$im,0,0,0,0,50,51,imagesx($im),imagesy($im)) or die("cant resampled");			
			
			$dst_image_dir = $dst_image_dir.$new_image;						
			imagegif($dst_im,$dst_image_dir);
			imagedestroy($im);
			imagedestroy($dst_im);
		
		}
}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Nov 29, 2006 2:52 am
by onion2k
The two lines you've commented out of imagecreatetruecolortransparent() are quite important, they're what makes the image transparent.

Posted: Wed Dec 13, 2006 8:41 pm
by nir.slsk
hi, i tried taking the advice above to thumbnail images on my website but i'm still getting black backgrounds in transparent gifs. i'm using the original version of imagecreatetruecolortransparent:

Code: Select all

function imagecreatetruecolortransparent($x,$y)
{
  $i = imagecreatetruecolor($x,$y);
  $b = imagecreatefromstring(base64_decode(blankpng()));
  imagealphablending($i,false);
  imagesavealpha($i,true);
  imagecopyresized($i,$b,0,0,0,0,$x,$y,imagesx($b),imagesy($b));
  return $i;
}
and this is my own code using it:

Code: Select all

$src_img=imagecreatefromgif($name);
  $dst_img=imagecreatetruecolortransparent($thumb_w,$thumb_h);

  imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

  imagegif($dst_img,$filename); 

  imagedestroy($dst_img); 
  imagedestroy($src_img);
what am i doing wrong? i know next to nothing about image processing and what each GD function does so i'm just copying what i see. thanks much, nir

Posted: Thu Dec 14, 2006 4:18 am
by onion2k
The problem is due to a combination of using imagecopyresampled(), and saving out to a gif afterwards. Gif files are pallette based, and can only have 1 transparency channel. When you copy an image with imagecopyresampled() it'll wipe out the current transparency channel and replace it with an alpha channel of per pixel transparency information. If you saved a PNG file it'd work ok (obviously you'd need the usual htc hack to get PNG transparency working in IE6), but as you're saving as a gif you're losing the transparency. Normally you'd convert back to a color pallette using imagetruecolortopallette() but that throws up a couple of issues - first, it's a crap function that does a really bad job of choosing colours, and second, as you've used imagecopyresampled() you're not going to have a single colour you can set as transparent any more.

The solution is to use imagecreate() in the first place to make an 8bit image canvas to copy to, and to use imagecopyresized() to copy the data. It will end up being nasty and blocky, but that's the price you pay for using gifs.

Posted: Thu Dec 14, 2006 1:07 pm
by nir.slsk
ah i see, i guess i'll go with black backgrounds then :) thank you very much for your help.