Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put PNG over a JPG in PHP [duplicate]

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

like image 351
Chris Avatar asked Feb 15 '10 22:02

Chris


2 Answers

<?
$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);
?>
like image 120
skyman Avatar answered Sep 24 '22 03:09

skyman


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);
like image 25
webkul Avatar answered Sep 25 '22 03:09

webkul