ekstrakt
ekstrakt

Reputation: 910

Transparent PNG over JPG in PHP

What seems to be simple, isn't :(

I'm trying to add something like a watermark (transparent png) on an image (jpg). This is the code I'm using:

$width = 800; 
$height = 600; 
$bottom_image = imagecreatefromjpeg("portrait1.jpg"); 
$top_image = imagecreatefrompng("man2.png"); 
imagesavealpha($top_image, true); 
imagealphablending($top_image, true); 
imagecopy($bottom_image, $top_image, 200, 200, 0, 0, $width, $height); 
header('Content-type: image/png');
imagepng($bottom_image);

When I merge the images, the png is positioned at the right place, everythig above and left of it is good (jpg is copied), but everything else is black.

I've tried setting imagesavealpha and imagealphablending to false, there wasn't any difference.

You can see the resulting image at http://ekstrakt.selfip.com/photobomb/image.php

I've searched around the net, I can't find a solution.

Any help is appreciated.

Upvotes: 4

Views: 8930

Answers (2)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324600

Your $width and $height should be the dimensions of the watermark, not of the photo. What you're telling it to do is copy the watermark with a much bigger size than it is. When it reads part of an image that doesn't exist (coordinates out of bounds) the result is opaque black, giving the result you see.

Upvotes: 3

Peter
Peter

Reputation: 16915

Use imagecopymerge() instead of imagecopy()

U may also like imagesavealpha()

Upvotes: 2

Related Questions