PHP - ImageCopyMerge 分层图像
PHP - ImageCopyMerge layered images
有人可以指出我问题的正确方向吗? :)
我尝试做的是将两个图像合并为一个图像,其中一个是 PNG 作为目标,中间有一个透明形状,另一个是 JPG 作为源,我希望它位于 PNG 图像的背面并通过透明形状看到形状。
像这样:
源图片:
目标图片:
想要的结果:
以下是我到目前为止尝试过但没有用的方法:
$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......
我试过反之亦然,但透明星形看起来是白色或棕色像素颜色。
我已经用这个函数做到了
imagecopymerge_alpha
这是代码
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);
有人可以指出我问题的正确方向吗? :)
我尝试做的是将两个图像合并为一个图像,其中一个是 PNG 作为目标,中间有一个透明形状,另一个是 JPG 作为源,我希望它位于 PNG 图像的背面并通过透明形状看到形状。
像这样:
源图片:
目标图片:
想要的结果:
以下是我到目前为止尝试过但没有用的方法:
$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......
我试过反之亦然,但透明星形看起来是白色或棕色像素颜色。
我已经用这个函数做到了 imagecopymerge_alpha
这是代码
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);