PHP and GD: PNG-24 transparency problem
I have this code for placing a transparent PNG image watermark on photos. It works fine as long as the watermark img is PNG-8 (but looks ugly with anti-aliased images). If I use PNG-24, the watermark image looks fine, but loses transparency. Any way to fix it? Thanks.
CODE:
header('content-type: image/jpeg');
$watermark = imagecreatefrompng('wm1.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
imagealphablending($watermark, true);
imagesavealpha($watermark, true);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg('image1.jpg');
$size = getimagesize('image1.jpg');
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $wate开发者_开发百科rmark_height - 5;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagejpeg($image, null, 95);
imagedestroy($image);
imagedestroy($watermark);
PNG 24 does not have a transparency layer, it just has 3 bytes representing Red Green and Blue. You should be using PNG 32 if possible as it has the alpha layer that you want.
Check out this link to see what I mean: http://www.deepbluesky.com/blog/-/the-difference-between-png24-and-png32_49/
as a Side note, this line of code in your program does nothing as $image
is clobbered on the very next line:
$image = imagecreatetruecolor($watermark_width, $watermark_height);
精彩评论