Can someone point me to the right direction in my question, please? :)
What I try to do is merge two images in one that is PNG as destination with a transparent shape in the middle and a JPG as source that I want to be in the back of the PNG image and seen trough the transparent shape.
Something like this:
Source image:
Destination image:
Desired result:
Here is what i have tried so far but is not working:
$dest = Imagecreatefrompng('img/dest_bg.png');
$src = Imagecreatefromjpeg('img/src.jpg');
Imagealphablending($dest, true);
Imagealphablending($src, true);
Imagesavealpha($dest, true);
Imagecopymerge($dest, $src, 200, 0, 0, 0, 400, 415, 100);
imagepng($dest......
I have tried viceversa, but the transparent star shape apears white or a brown pixeled color.
I've managed to do this with this function imagecopymerge_alpha
here is the code
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
// creating a cut resource
$cut = imagecreatetruecolor($src_w, $src_h);
// copying relevant section from background to the cut resource
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
// copying relevant section from watermark to the cut resource
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
// insert cut resource to destination image
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}
$dest = Imagecreatefrompng('img/final_bg.png');
$src2 = Imagecreatefromjpeg('uploads/picture.jpg');
$src = Imagecreatefrompng('img/pink_bg.png');
Imagealphablending($dest, true);
Imagealphablending($src, true);
Imagesavealpha($dest, true);
Imagecopymerge($src, $src2, 310, 120, 0, 0, 200, 200, 100); // i have positioned a small picture on a color filled background in the middle because the final background, witch is a picture with a transparent shape in the middle
imagepng($src, 'uploads/temp.png');
Imagedestroy($src2);
Imagedestroy($src);
$temp = Imagecreatefrompng('uploads/temp.png');
Imagealphablending($temp, false);
Imagecopymerge_alpha($temp, $dest, 0, 0, 0, 0, 800, 420, 100); //merged the color filled background with the picture on it with the final background here..
imagejpeg($temp, 'uploads/final_result.jpg');
Imagedestroy($dest);
Imagedestroy($temp);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.