Can't Keep PNG Transparency with GD Library Watermark

First off, here’s my code thanks in part to Sitepoint:


$watermark = imagecreatefrompng('watermark.png');

imagealphablending($watermark, false);
imagesavealpha($watermark,true);

$watermark_width = imagesx($watermark);

$watermark_height = imagesy($watermark);

$image = imagecreatetruecolor($watermark_width, $watermark_height);


imagealphablending($image, false);
imagesavealpha($image,true);

$image = imagecreatefromjpeg($_GET['src']);

$size = getimagesize($_GET['src']);

$dest_x = $size[0] - $watermark_width - 5;

$dest_y = $size[1] - $watermark_height - 5;

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
header('content-type: image/jpeg');
imagejpeg($image);

imagedestroy($image);

imagedestroy($watermark);

Here is a sample of what it creates:

http://www.traileraddict.com/fm/watermark.php?src=http://cdn.traileraddict.com/content/walt-disney-pictures/big_hero_six.jpg

Watermark: http://www.traileraddict.com/fm/watermark.png
That white-barred watermark should be transparent, but it is not. If I do PNG-8 I get the best transparency it can make, but with PNG-24 with alpha (so should be PNG-32), it should be working fine also.

I cannot figure out why it is fighting me. All feedback appreciated. GD Version bundled (2.0.34 compatible) with png version 1.5.14

All feedback greatly appreciated.

Cheers!
Ryan

That’s because you are writing it as a jpeg which to my knowledge doesn’t support transparency. You should use imagepng (and change your content type to image/png)

Thanks. I made the adjustments and that does not work; the adjustments remain and you can see new header. I want the final product to be a jpeg, but when I merge the PNG with transparency layer over the sourced JPEG when creating a new image, it’s a watermark, that’s when the alpha transparency fails, long before the final jpeg output.

I could have the watermark.png be PNG-8 and it works fine, save for the face that PNG-8 is a bit rough around the edges.

It’s almost like my PNG-32 isn’t really a PNG-32 or the alpha layer isn’t readying right.

Cheers!
Ryan

I had an online program export the PNG with alpha and now the image has a black background instead of white when merged over the sourced image.

Okay, I think I got it working
http://archive.cpradio.org/test/watermark.php

<?php
$watermark = imagecreatefrompng('watermark.png');   

// no idea what the following two lines are trying to do... :/
#imagealphablending($watermark, false); 
#imagesavealpha($watermark,true); 

$watermark_width = imagesx($watermark);   
$watermark_height = imagesy($watermark);   

// these get overwritten by the imagecreatefromjpeg, thus therefore useless
#$image = imagecreatetruecolor($watermark_width, $watermark_height);   
#imagealphablending($image, false); 
#imagesavealpha($image,true);  

$image = imagecreatefromjpeg('big_hero_six.jpg');   

$size = getimagesize('big_hero_six.jpg');   

$dest_x = $size[0] - $watermark_width - 5;   

$dest_y = $size[1] - $watermark_height - 5;   

// since you are using 100% alpha, why bother with that parameter?
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);   
header('content-type: image/jpeg');  
imagejpeg($image);   

imagedestroy($image);   
imagedestroy($watermark);

Ok, don’t take this the wrong way, but I want to make sweet, sweet… sandwiches with you right now. That worked perfect.

Cheers!

:slight_smile: Glad to hear it.

Thank you so much, you really saved my time!