Resizing transparent PNG
Posted: Thu Jan 26, 2006 4:52 am
I created a simple PNG-8 image to use as a watermark with GD. It's basically just some text with completely transparent background. I can easily set the PNG's transparency with imagecopymerge() when merging it with other images. The problem comes when I resize the PNG at runtime and use the resized version as the watermark. Here's how it looks when I apply it. You can easily notice the white-ish background it gets after the resize. It should be completely transparent. This doesn't happen when using the original (non-resized) PNG. How do I get rid of the white background?
Here's the code:
Here's the code:
Code: Select all
$image = imagecreatefromjpeg('booh.jpg'); //image to be watermarked
list($image_width, $image_height) = getimagesize('booh.jpg');
$water = imagecreatefrompng('text.png'); //watermark
list($water_width, $water_height) = getimagesize('text.png');
$water_resized_width = $water_width * 0.5; //resized width
$water_resized_height = $water_height * 0.5; //resized height
$resized = imagecreatetruecolor($water_resized_width, $water_resized_height);
$transparent = imagecolorallocatealpha($resized, 255, 255, 255, 127);
imagefill($resized, 0, 0, $transparent);
imagecopyresampled($resized, $water, 0, 0, 0, 0, $water_resized_width, $water_resized_height, $water_width, $water_height);
imagecopymerge($image, $resized, (($image_width - $water_resized_width) / 2), (($image_height - $water_resized_height) / 2), 0, 0, $water_resized_width, $water_resized_height, 15);
imagejpeg($image, 'finished.jpg', 100);
imagedestroy($image);
imagedestroy($water);
imagedestroy($resized);