PHP - 使用imagecopy时颜色不正确

4

我有几个PNG图像是这样生成的:

$img = imagecreatefrompng($full_path_to_file);
imagealphablending($img , true); // setting alpha blending on
imagesavealpha($img , true); // save alphablending setting

这些图片的颜色和透明背景都很好。

我需要将这些图片合并成一个。为此,我执行以下操作:

  1. 创建一个正确尺寸的空白图像

    $full_image = imagecreate($full_width, $full_height);

  2. 逐个将png图像复制到空白图像上

    imagecopy($full_image, $src, $dest_x, $dest_y, 0, 0, $src_width, $src_height)

图片已经成功合并。背景是透明的,但是颜色不正确

我该如何确保获得正确的颜色?

更新: 如建议所述,修复方法是使用imagecreatetruecolor。此外,我需要将第二个参数设置为imagealphablending的false。因此,在创建png图像和创建full_image时,我调用:

imagealphablending($img , false); // updated to FALSE
imagesavealpha($img , true); 
imagesavealpha的文档中写道:

您需要取消图像混合(imagealphablending($im, false))才能使用它。


你能展示一下你所说的“不正确”是什么意思吗? - Michael Irigoyen
你用的不是CMYK文件吗?你不会是第一个这样的人。 - Martijn
2个回答

4

尝试过了,没有任何作用。imagecreatetruecolor是用于JPEG格式的。 - dev.e.loper
2
你尝试过使用imagecopymerge将两个PNG图像合并到创建的imagecreatetruecolor中,然后使用imagepng输出吗? - stu236
一秒钟。我实际上尝试了 imagecreatetruecolor,这次它给了我正确的颜色,但背景是黑色而不是透明的。 - dev.e.loper
使用imagecreatetruecolor创建的图像上使用alpha通道即可解决问题。 - stu236
需要将 alpha 混合设置为 false。更新后的答案包含更多细节。 - dev.e.loper

0

只需要在调用imagecopy()方法后添加以下代码:

$white = imagecolorallocate($destination_image, 255, 255, 255); imagefill($destination_image,0,0,$white);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接