简体   繁体   English

PHP中的JPG透明PNG

[英]Transparent PNG over JPG in PHP

What seems to be simple, isn't :( 什么似乎很简单,不是:(

I'm trying to add something like a watermark (transparent png) on an image (jpg). 我试图在图像(jpg)上添加类似水印(透明png)的东西。 This is the code I'm using: 这是我正在使用的代码:

$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);

When I merge the images, the png is positioned at the right place, everythig above and left of it is good (jpg is copied), but everything else is black. 合并图像时,png位于正确的位置,上面和左边的每个都很好(jpg被复制),但其他一切都是黑色的。

I've tried setting imagesavealpha and imagealphablending to false, there wasn't any difference. 我已经尝试将imagesavealpha和imagealphablending设置为false,没有任何区别。

You can see the resulting image at http://ekstrakt.selfip.com/photobomb/image.php 您可以在http://ekstrakt.selfip.com/photobomb/image.php上看到生成的图像

I've searched around the net, I can't find a solution. 我在网上搜索,我找不到解决方案。

Any help is appreciated. 任何帮助表示赞赏。

Your $width and $height should be the dimensions of the watermark, not of the photo. 你的$width$height应该是水印的尺寸,而不是照片的尺寸。 What you're telling it to do is copy the watermark with a much bigger size than it is. 你告诉它要做的是复制比它大得多的水印。 When it reads part of an image that doesn't exist (coordinates out of bounds) the result is opaque black, giving the result you see. 当它读取不存在的图像的一部分(坐标超出界限)时,结果是不透明的黑色,给出您看到的结果。

Use imagecopymerge() instead of imagecopy() 使用imagecopymerge()代替imagecopy()

U may also like imagesavealpha() 你可能也喜欢imagesavealpha()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM