Put PNG over a JPG in PHP
55,874
Solution 1
<?
$png = imagecreatefrompng('./mark.png');
$jpeg = imagecreatefromjpeg('./image.jpg');
list($width, $height) = getimagesize('./image.jpg');
list($newwidth, $newheight) = getimagesize('./mark.png');
$out = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($out, $jpeg, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagecopyresampled($out, $png, 0, 0, 0, 0, $newwidth, $newheight, $newwidth, $newheight);
imagejpeg($out, 'out.jpg', 100);
?>
Solution 2
This is the working code which i using
$dest = imagecreatefrompng('mapCanvas.png');
$src = imagecreatefromjpeg('si.jpg');
imagealphablending($dest, false);
imagesavealpha($dest, true);
// Copy and merge
imagecopymerge($dest, $src, 17, 13, 0, 0, 60, 100, 100);
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
Solution 3
Here is a link to an example that will overlay a transparent watermark onto an image. Might be your use case, might be related.
http://www.php.net/manual/en/image.examples.merged-watermark.php
There is also a way to load JPG images, resize images, turn on alpha tracking, and export images in GD.
Jacob
Author by
Chris
Updated on July 09, 2022Comments
-
Chris almost 2 years
I want to do the following in PHP:
I have two images, a jpg and a png. I want to resize the jpg to the same size as the png then put the png on top. The PNG has transparency so I would like to preserve that so the jpg shows underneath.
If anyone could help that would be great!
Thanks
-
Paul T. Rawkeen over 11 yearsThanks a lot! Stuck on the similar merging issue for 3rd day without any ideas (forgot everything what knew before) ... saved my soul! Great thanks! God bless you!
-
Anupal over 10 yearsCode is not working. As it displays image not found icon.
-
Lloyd Erasmus about 6 yearsGreat answer, solution works well.
-
Deepak Chawla over 5 yearsThks it is working perfectly but output image have n colours means it outputs black-white image. Now what to do for it.