PHP 中的透明 PNG 转 JPG
看起来很简单,其实不然:(
我正在尝试在图像(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< 处查看生成的图像/a>
我在网上搜索过,找不到解决方案。
任何帮助表示赞赏。
What seems to be simple, isn't :(
I'm trying to add something like a watermark (transparent png) on an image (jpg).
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.
I've tried setting imagesavealpha and imagealphablending to false, there wasn't any difference.
You can see the resulting image at http://ekstrakt.selfip.com/photobomb/image.php
I've searched around the net, I can't find a solution.
Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
$width
和$height
应该是水印的尺寸,而不是照片的尺寸。你告诉它要做的就是复制比实际尺寸大得多的水印。当它读取不存在的图像部分(坐标超出范围)时,结果是不透明的黑色,给出您看到的结果。Your
$width
and$height
should be the dimensions of the watermark, not of the photo. 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.使用 imagecopymerge() 而不是
imagecopy()
U可能还喜欢 imagesavealpha()
Use imagecopymerge() instead of
imagecopy()
U may also like imagesavealpha()