Forum Moderators: coopster

Message Too Old, No Replies

Help adding transparency to GD script

         

WoodNotOil

1:00 am on Mar 13, 2011 (gmt 0)

10+ Year Member



I have a piece of code for a function to generate thumbnails that I found here:[icant.co.uk ]. It works really well, but if the image is a png and has a transparent background, it turns the background black. I am not familiar enough with the GD library to figure out how to add transparency support. Any ideas?

/*
Function createthumb($name,$filename,$new_w,$new_h) Example: createthumb($p,"tn_".$p,150,150);
creates a resized image
variables:
$nameOriginal filename
$filenameFilename of the resized image
$new_wwidth of resized image
$new_hheight of resized image
*/
function createthumb($name,$filename,$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);}
$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);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}

WoodNotOil

3:10 pm on Mar 14, 2011 (gmt 0)

10+ Year Member



I have tried adding the following, but I am still just getting a black background. Anyone have any other ideas?

$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagealphablending($dst_img,false);
imagesavealpha($dst_img,true);
$transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);
imagefilledrectangle($dst_img, 0, 0, $thumb_w, $thumb_h, $transparent);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

rocknbil

3:37 pm on Mar 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ImageCreateTrueColor defaults to black BG, you need to create a white transparent BG.

$transparent_bg = ImageColorAllocateAlpha($my_background, 255, 255, 255, 127);
ImageFill($my_background, 0, 0 , $bg);

Then merge it.

ImageCopyMerge($my_background, $dst_img, 0, 0, 0, 0, $width, $height, $filename);

Something like that . . . play with it a bit.

WoodNotOil

4:10 pm on Mar 14, 2011 (gmt 0)

10+ Year Member



Isn't that what this is supposed to do?

$transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);
imagefilledrectangle($dst_img, 0, 0, $thumb_w, $thumb_h, $transparent);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

WoodNotOil

5:59 pm on Mar 14, 2011 (gmt 0)

10+ Year Member



It is working now. My second code was correct. My issue was in file naming (outside of this script). My image url kept pointing to the original thumbnail with the background instead of the new one being created (which had been renamed). Thanks for the help though...