Combine png ontop from jpg and keep transparency with php

I have a PNG and I'm trying to merge it on top of a jpg. With the following code

$dest = imagecreatefromjpeg("example.jpg");
$src = imagecreatefrompng("example.png");

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

imagealphablending($src, true);

imagecopymerge($dest, $src, $src2x, $src2y, 0, 0, $src2w, $src2h, 100);

header('Content-Type: image/png');
imagepng($dest, "user/".$imei."/".$picCount."_m");

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

The results are as follows.

enter image description here

I also tried a suggestion from a similar question , which said that use 'imagecopyresampled' isntead 'imagecopymerge', but when I did this, the Santa hat did not show at all.

What do I need to change to make the santa hat transparent when combined?

+5
source share
1 answer

Solution required as using 'imagecopyresampled'. Also remove lines 4 and 5 from the published source code.

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

Here is the full working version

$dest = imagecreatefromjpeg("example.jpg");
$src = imagecreatefrompng("example.png");

imagecopyresampled($dest, $src, $src2x, $src2y, 0, 0, $src2w, $src2h, $src2w, $src2h); 

header('Content-Type: image/png');
imagejpeg($dest, "user/".$imei."/".$picCount."_m.jpeg");

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

enter image description here

+10
source

All Articles