【发布时间】:2014-10-15 21:42:26
【问题描述】:
我有一个图像堆叠过程,我正在尝试使用 PHP GD 来完成。我有以下内容:
3 张图片:
- 蒙面剪贴画
- 剪贴画纹理
- 最终背景
我蒙版的剪贴画在纹理应用到剪贴画后的透明度位置是黑色的,并且对于在纹理上分层是透明的。
以下代码适用于此:
$im = imagecreatetruecolor($width,$height);
imagecopy($im, $texture, 0, 0, 0, 0, $width, $height);
imagecopy($im, $clipart, 0, 0, 0, 0, $width, $height);
imagecolortransparent($im, imagecolorclosest($clipart, 0, 0, 0));
输出是具有透明背景的图像和应用了纹理的剪贴画。
但是,当我在 Photoshop 中打开该文件时,透明区域是黑色的,这也将我带到了此功能的其余部分:
现在我有了这个图像,我需要将它叠加在“最终背景”图像的顶部,这将使之前输出的所有透明度现在都是这个“最终背景”纹理。我的想法是这样的:
$im = imagecreatetruecolor($width,$height);
imagecopy($im, $texture, 0, 0, 0, 0, $width, $height);
imagecopy($im, $clipart, 0, 0, 0, 0, $width, $height);
imagecolortransparent($im, imagecolorclosest($clipart, 0, 0, 0));
$img = imagecreatetruecolor($width,$height);
imagecopy($img, $background, 0, 0, 0, 0, $width, $height);
imagecopy($img, $im, 0, 0, 0, 0, $width, $height);
imagedestroy($im);
return $img;
这个问题是输出带有黑色背景的图像,而不是我的最终纹理层。我相信这个用于最终分层的代码实际上可以正常工作,并且最终输出和 Photoshop 中的黑色背景是由于在第一部分中缺少一些 alpha 线。我试着玩弄:
imagealphablending( $im, false );
imagesavealpha( $im, true );
混合和匹配真/假并仅使用其中一个进行交替似乎并不重要。
如果有人能在这里解释我的错误,将不胜感激。
更新
代码:
// Layer clipart over texture and convert black to transparent (works)
$im = imagecreatetruecolor($width,$height);
imagecopy($im, $texture, 0, 0, 0, 0, $width, $height);
imagecopy($im, $clipart, 0, 0, 0, 0, $width, $height);
imagecolortransparent($im, imagecolorclosest($clipart, 0, 0, 0));
// Layer above image with transparency over background (non-working)
$img = imagecreatetruecolor($width,$height);
imagecopy($img, $background, 0, 0, 0, 0, $width, $height);
imagecopy($img, $im, 0, 0, 0, 0, $width, $height);
header('Content-Type: image/png');
//imagepng($im); // Correctly outputs first step
imagejpeg($img); // Incorrectly outputs final result
imagedestroy($im);
imagedestroy($img);
【问题讨论】:
-
请编辑您的问题以包含三张图片的链接。 (我可以用自己制作的图像进行测试,但我不知道您的源图像是什么格式。)
-
@timclutton 感谢您对此进行调查。我更新了我的回复,将所有图片和完整的测试代码包含在一个 zip 文件中
-
对于延迟发布答案表示歉意;其他事件超过了我!