Lately I had to write a PHP function that generates differently sized thumbnails from an image with GD Lib. Thereby I had lots of trouble with semi-transparent PNG graphics. Although I found many tips on the internet that pointed me in the right direction, I couldn’t find a really complete and neat solution. Many people emphasized the oh so big importance of the functions
imagesavealpha(), which – as it later transpired – were completely meaningless for my purposes.
Well, I’ll write the solution down here as usual, in case somebody else is looking for it as desperately as I did – or in case I forget it. ;) The pretty red dot here on the right will serve as visualization.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$filename = "rotverlauf.png"; $oldimg = imagecreatefrompng($filename); $oldwidth = imagesx($oldimg); $oldheight = imagesy($oldimg); $newwidth = $oldwidth/2; $newheight = $oldheight/2; $newimage = imagecreate($newwidth, $newheight); $color = imagecolorallocatealpha($newimage, 0, 0, 0, 127); imagefill($newimage, 0, 0, $color); imagecopyresampled($newimage, $oldimg, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);
The trick is to first create a new image (
$newimage) and fill it with a transparent background color (
$color). If you now copy the original image into it with the help of
imagecopyresampled(), the transparency will be preserved.
The example script reduces the red dot to half its size. The result can be seen on the left.
But I had yet another problem: I wanted to save the thumbnails of PNG graphics in the JPEG format, too, i. e. put them on a white or black background, while the color gradient should of course be preserved. After a few unsuccessful attempts, the solution turned out to be quite simple. ;) You just have to replace the functions
$newimage = imagecreatetruecolor($newwidth, $newheight); $color = imagecolorallocate($newimage, 255, 255, 255);
As you can see, I chose white (255, 255, 255) as background color this time, because it looks better than black in most of the cases.
Actually I could have used
imagecreatetruecolor() in the first example, too, when creating a PNG. But some servers (like mine) seem to have problems with that, so you should rather stick with
imagecreate() when making PNGs.
On the right you can now see the fantastic red dot as JPEG.
Now that the new image is ready, you can either save it to a file:
imagepng($nm, "neuer-dateiname.png"); //bzw. imagejpeg();
or output it directly:
header("Content-type: image/png"); //bzw. image/jpg imagepng($nm); //bzw. imagejpeg();