fightstarr20
fightstarr20

Reputation: 12618

PHP GD Overlay transparent PNG on top of JPG

I am trying to use PHP GD to overlay a frame onto an image. Here is an example of my source images and what I am trying to achieve...

The frame image is a transparent png, my code looks like this...

$dest = imagecreatefromjpeg('image.jpg');
$src = imagecreatefrompng('frame.png');

imagecopymerge($dest, $src, 0, 0, 0, 0, 300, 300, 50);

header('Content-Type: image/jpeg');

imagejpeg($dest, 'output.jpg');

imagedestroy($dest);
imagedestroy($src);

My output image looks just like the middle one with no frame. Can anyone spot anything obvious I am doing wrong?

Upvotes: 0

Views: 2421

Answers (1)

Gerrit Fries
Gerrit Fries

Reputation: 143

You should take a closer look at the documentation of imagecopymerge(). imagecopymerge() expects exactly 9 parameters, 10 are given in your script.

Edit: imagecopymerge() can not handle alpha channels itself. You have to add some extra lines of code. Here is what it should look like:

$src = imagecreatefromjpeg('image.jpg');
$dest = imagecreatefrompng('frame.png');

imagealphablending($dest, false);
imagesavealpha($dest, true);
imagealphablending($src, false);
imagesavealpha($src, true);

$insert_x = imagesx($src); 
$insert_y = imagesy($src);

$white = imagecolorallocatealpha($dest, 255, 255, 255, 127); 
imagecolortransparent($dest, $white);
imagecopymerge($src, $dest, 0, 0, 0, 0, $insert_x, $insert_y, 100); 

header('Content-Type: image/jpeg');
imagejpeg($src);

imagedestroy($dest);
imagedestroy($src);

Upvotes: 2

Related Questions