PHP中PNG透明图覆盖在JPG上

4
我会尽力帮助您翻译。以下是内容的翻译:

看起来很简单,但并不是 :(

我正在尝试在图像(jpg)上添加类似于水印(透明png)的东西。这是我正在使用的代码:

$width = 800; 
$height = 600; 
$bottom_image = imagecreatefromjpeg("portrait1.jpg"); 
$top_image = imagecreatefrompng("man2.png"); 
imagesavealpha($top_image, true); 
imagealphablending($top_image, true); 
imagecopy($bottom_image, $top_image, 200, 200, 0, 0, $width, $height); 
header('Content-type: image/png');
imagepng($bottom_image);

当我合并这些图像时,png图像被放置在正确的位置,其上方和左侧的所有内容都很好(jpg被复制),但其他所有内容都是黑色的。
我尝试将imagesavealpha和imagealphablending设置为false,但没有任何区别。
您可以在http://ekstrakt.selfip.com/photobomb/image.php查看结果图像。
我在网上搜索过,但找不到解决方案。
感谢您的任何帮助。

2
你还需要在$bottom_image上启用alpha混合。 - Marc B
2个回答

3

你的$width$height应该是水印的尺寸,而不是照片的尺寸。你现在的做法是让它复制比水印本身尺寸更大的图片。当它读取不存在的部分(超出边界的坐标)时,结果会变成不透明黑色,这就是你看到的效果。


2

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