将2个透明PNG图像与PHP GD库合并
我正在尝试合并两个透明的PNG图像。第二个PNG图像的不透明度应为50% 问题:将2个透明PNG图像与PHP GD库合并,php,image,image-processing,png,transparency,Php,Image,Image Processing,Png,Transparency,我正在尝试合并两个透明的PNG图像。第二个PNG图像的不透明度应为50% 问题: <?php function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ $cut = imagecreatetruecolor($src_w, $src_h); imagecopy($cut, $dst_im, 0, 0, $dst_x, $ds
<?php
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
$cut = imagecreatetruecolor($src_w, $src_h);
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}
$image1 = imagecreatefrompng('a.png'); //300 x 300
$image2 = imagecreatefrompng('b.png'); //150 x 150
$merged_image = imagecreatetruecolor(300, 300);
imagealphablending($merged_image, false);
imagesavealpha($merged_image, true);
imagecopy($merged_image, $image1, 0, 0, 0, 0, 300, 300);
imagecopymerge_alpha($merged_image, $image2, 0, 0, 0, 0, 150, 150, 50);
header('Content-Type: ' . image_type_to_mime_type(IMAGETYPE_PNG));
imagepng($merged_image);
?>
如果第二个PNG仅覆盖第一个PNG的不透明像素,则一切正常
如果第二个PNG具有覆盖第一个PNG的透明像素的区域,则这些区域将变为黑色
代码:
<?php
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
$cut = imagecreatetruecolor($src_w, $src_h);
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}
$image1 = imagecreatefrompng('a.png'); //300 x 300
$image2 = imagecreatefrompng('b.png'); //150 x 150
$merged_image = imagecreatetruecolor(300, 300);
imagealphablending($merged_image, false);
imagesavealpha($merged_image, true);
imagecopy($merged_image, $image1, 0, 0, 0, 0, 300, 300);
imagecopymerge_alpha($merged_image, $image2, 0, 0, 0, 0, 150, 150, 50);
header('Content-Type: ' . image_type_to_mime_type(IMAGETYPE_PNG));
imagepng($merged_image);
?>
如果你改为试试这个怎么样?它对我有用
<?php
$image1 = imagecreatefrompng('a.png'); //300 x 300
$image2 = imagecreatefrompng('b.png'); //150 x 150
$merged_image = imagecreatetruecolor(300, 300);
// DEFINE MAGENTA AS THE TRANSPARENCY COLOR AND FILL THE IMAGE FIRST
$transparentColor = imagecolorallocate($merged_image, 255, 0, 255);
imagecolortransparent($merged_image, $transparentColor);
imagefill($merged_image, 0, 0, $transparentColor);
imagesavealpha($merged_image, true);
imagealphablending($image1, false);
imagecopyresampled($merged_image, $image1, 0, 0, 0, 0, 300, 300, 300, 300);
imagealphablending($image1, true);
imagedestroy($image1); // FREE UP SOME MEMORY
imagealphablending($image2, false);
imagecopyresampled($merged_image, $image2, 0, 0, 0, 0, 300, 300, 150, 150);
imagealphablending($image2, true);
imagedestroy($image2); // FREE UP SOME MEMORY
imagealphablending($merged_image, false);
imagesavealpha($merged_image, true);
header('Content-Type: ' . image_type_to_mime_type(IMAGETYPE_PNG));
imagepng($merged_image);
?>
在这方面我做了很多实验。我希望这能帮助您或其他可能偶然发现这一点的人。您能提供一些代码来显示您的尝试吗?这个问题似乎没有解决方法吗?似乎在添加透明图像之前,在将源图像复制到目标图像之前,应将imagealphablending设置为false,然后在复制后将其设置为true。我不知道为什么会这样,但确实如此。