Home > Mobile >  Merge JPG image with PNG with transparent background
Merge JPG image with PNG with transparent background

Time:08-02

I need to merge two images using PHP. The background image is JPG and the second image is PNG and has a transparent background. I've tried several ways and suggestions that I've seen on the internet, but none works. See what my code looks like:

$audible_fundo = imagecreatefromjpeg('audible.jpg');
$audible_icon = imagecreatefrompng('icon.png');



imagealphablending($audible_icon, false);
imagesavealpha($audible_icon, true);
imagecolortransparent($audible_icon);

imagecopymerge($audible_fundo, $audible_icon, 700, 300, 0, 0, 479, 180, 100);


imagepng($audible_fundo);

See the result: Image Merge Result

The image on the right (png) has a white background and a black section. I need to leave it with the transparent background, like the original. How can I resolve this?

CodePudding user response:

Those Line Of Code may you help. You can Use PNG Image above the base JPG Image

<?php

$WatermarkImage = imagecreatefrompng('logo.png'); // Transparent Background 
must be png
$BaseImage = imagecreatefromjpeg('my.jpg');


$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($WatermarkImage);
$sy = imagesy($WatermarkImage);

imagecopy($BaseImage, $WatermarkImage, imagesx($BaseImage) - $sx - 
$marge_right, imagesy($BaseImage) - $sy - $marge_bottom, 0, 0, 
imagesx($WatermarkImage), imagesy($WatermarkImage));


header('Content-type: image/png');
imagepng($BaseImage);
imagedestroy($BaseImage);
?>

Any question ask me.

  • Related