2

This seems to be a common question, but trying all the combinations, I have found, it still is not working for me.

My PNG watermark is transparent and I wish to overlay the original JPG with this watermark and add a 50% opacity to the watermark.

The watermark is added and the opacity created, but the transparency of the PNG is rendered as opaque white.

I have seen examples using imagecopy() but that function does not have the option to add opacity.

My code is as follows:

<?php
$file = 'orgCar.jpg';
$newfile = 'newCar.jpg';

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
$tempIMG = imagecreatefromjpeg($newfile);
$wmkIMG = imagecreatefrompng('wmark.png');
imagealphablending($wmkIMG,true);

imagecopymerge($tempIMG, $wmkIMG, 755, 864, 0, 0, 465, 36, 50);

// Save the image to file and free memory
imagejpeg($tempIMG,'newWM.jpg');
imagedestroy($orgIMG);
imagedestroy($wmkIMG);

echo '<h3>Testing of Watermarking</h3>';
echo '<div>';
echo '<img width="160" src="orgCar.jpg" title="original" alt"" />';
echo '<img width="160" src="newCar.jpg" title="copy" alt"" />';
echo '<img width="240" src="wmark.png" title="watermark" alt"" /><br>';
echo '<img width="640" src="newWM.jpg" title="New with Watermark" alt"" />';
echo '</div>';
?>

If there is a simple answer, I have overlooked, then I would be very grateful, if someone could point me to it.

hakre
  • 193,403
  • 52
  • 435
  • 836
mcl
  • 691
  • 2
  • 10
  • 23

1 Answers1

5

The problem is that amount of transparency that you pass to imagecopymerge() is used instead of the image alpha channel, not added to it.

You have two options:

1) Modify the PNG so it includes the 50% opacity and then use imagecopy() (I tested it and works fine). Remember that true-color PNG's can use use a full alpha channel so you can include semi-transparency in the image.

2) use a workaround like the one described here:

imagecopymerge_alpha

Alberto Martinez
  • 2,620
  • 4
  • 25
  • 28
  • I tried your excellent idea using Fireworks to reduce the opaqueness of my watermark. It reduced the intensity of the Watermark, but when I did the `imagecopy()` the transparency of the outside worked, but the main watermark did not have any opaqueness. Do i need to apply any GD command to my watermark before doing the `imagecopy()` or do I need to use a different program to create the opaque effect. – mcl Mar 11 '12 at 20:16
  • For the tests I used your code and replaced imagecopymerge() with imagecopy() first, and then with imagecopymerge_alpha() (both methods produce the same visual results). Maybe Fireworks saved the PNG as 8 bit (256 colors)? For full alpha channel you need to save the image as 32 bit, otherwise the PNG works just like a GIF image. You can check the PNG format of the image using Irfanview: http://www.irfanview.com/ (Image -> Information). – Alberto Martinez Mar 13 '12 at 22:21