Re: [GD-DEVEL] ok...now I know why .gifs turn black...what do I do about it

[email protected] ("Pierre Joye") Sat, 12 Apr 2008 09:35:58 +0200
Newsgroups php.gd.devel
Message-ID <[email protected]>
Hi Steve,

On Sat, Apr 12, 2008 at 9:19 AM, Steve Davis <[email protected]> wrote:
> Thanks for the prompt reply Pierre and I appreciate your help
>
>  I think it happens with any .GIF with transparent background - here's one
>  for sure
>
>  Original - http://davis.net.au/seal.gif
>  Thumb - http://davis.net.au/image-3-thumb.jpg
>
>  Ahh...I just saw a potential problem from the start I hadn't noticed.  I am
>  converting the .gif into a .jpg.  That is fine for what I am doing as long
>  as I can get a white bg.


JPEG has no notion of transparent color or background, that's why the
color is displayed (in this case, the black).

>  The PHP code:
>
>  function createthumb($name,$filename,$location,$new_w,$new_h){
>         $system=explode('.',$name);
>         if (preg_match('/jpg|jpeg/',$system[1])){
>                 $src_img=imagecreatefromjpeg($name);
>         }
>         if (preg_match('/png/',$system[1])){
>                 $src_img=imagecreatefrompng($name);
>         }
>                 if (preg_match('/gif/',$system[1])){
>                 $src_img=imagecreatefromgif($name);
>         }
>
>         $old_x=imageSX($src_img);
>         $old_y=imageSY($src_img);
>         if ($old_x > $old_y) {
>                 $thumb_w=$new_w;
>                 $thumb_h=$old_y*($new_h/$old_x);
>         }
>         if ($old_x < $old_y) {
>                 $thumb_w=$old_x*($new_w/$old_y);
>                 $thumb_h=$new_h;
>         }
>         if ($old_x == $old_y) {
>                 $thumb_w=$new_w;
>                 $thumb_h=$new_h;
>         }
>
>         $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);

Add this line here:

    imagefilledrectangle($dst_img, 0,0, $thumb_w -1 ,$thumb_h - 1,
imagecolorallocate($dst_img, 255,255,255))

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

That should solve the problem. Let us know if you need more
information or examples :)

Cheers,
-- 
Pierre
http://blog.thepimp.net | http://www.libgd.org