PHP 中的透明 PNG 转 JPG

发布于 2024-12-13 03:33:49 字数 766 浏览 5 评论 0原文

看起来很简单,其实不然:(

我正在尝试在图像(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我乃一代侩神 2024-12-20 03:33:49

您的 $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.

恏ㄋ傷疤忘ㄋ疼 2024-12-20 03:33:49

使用 imagecopymerge() 而不是 imagecopy()

U可能还喜欢 imagesavealpha()

Use imagecopymerge() instead of imagecopy()

U may also like imagesavealpha()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文