PHP - 水印 PNG 透明度/Alpha [英] PHP - Watermark PNG Transparency/Alpha

查看:44
本文介绍了PHP - 水印 PNG 透明度/Alpha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已经被覆盖了很多次,但我一直在尝试不同的脚本并修改我拥有的脚本,但我仍然无法让我的 PNG-24 watermark.png 透明我的父图像的顶部.

I know this has been covered PLENTY of times but I keep trying different scripts and modifying the one I have, and I still can't get my PNG-24 watermark.png to be transparent over the top of my parent image.

这是我目前拥有的:

<?

header('content-type: image/jpeg');

$watermark = imagecreatefrompng('watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);

$image = imagecreatefromjpeg($imageURL);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$size = getimagesize($imageURL);

imagealphablending($image, false);
imagesavealpha($image, true);

$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);

imagejpeg($image);

imagedestroy($image);
imagedestroy($watermark);

?>

我刚刚阅读了另一个 SO 问题,其中一个答案建议如果您不添加这两行,它将不透明:

I've just read another SO question and one of the answers advised that it won't be transparent if you don't add these two lines:

imagealphablending($image, false);
imagesavealpha($image, true);

我添加了它们,但仍然没有.我尝试将标题和输出设置为 PNG,但仍然没有运气.我在浏览器中加载了水印(原始),它绝对是透明的,但不是在图像上.这当然不会那么难吗?知道我做错了什么吗?

I added them and still not. I tried setting headers and output to PNG instead but still no luck. I loaded the watermark in my browser (raw) and it's definitely transparent but just not on the image. Surely this can't be that difficult? Any ideas what I'm doing wrong?

推荐答案

这不是应用程序代码的问题,而是水印图像 (PNG) 本身的问题.

It's not an issue with the application code, it's with the watermark image (PNG) itself.

很多水印示例/教程都说要使用 PNG-24 水印,但根据我刚刚读过的博客,他们说 imagecopymerge 不太处理 PNG-24 文件好吧,因此,请使用 PNG-8 和一些特殊的保存为 Web"设置.我这样做了,现在工作正常.

A lot of watermark examples/tutorials say to use a PNG-24 watermark, but according to a blog I've just read, they say that imagecopymerge does not deal with PNG-24 files very well, therefore, use PNG-8 and some special 'Save for Web' settings. I did this and it works fine now.

这是 这个博客:

水印图像应为以下推荐之一格式:

The watermark image should be in one of the following recommended formats:

  • PNG-8(推荐)
    颜色:256 或更少
    透明度:开/关
  • GIF
    颜色:256 或更少
    透明度:开/关
  • JPEG
    颜色:真彩色
    透明度:不适用

imagecopymerge 函数不能正确处理 PNG-24图片;因此不推荐使用.

The imagecopymerge function does not properly handle the PNG-24 images; it is therefore not recommended.

如果您使用 Adob​​e Photoshop 创建水印图像,则建议您使用带有以下内容的保存为 Web"命令设置:

If you are using Adobe Photoshop to create watermark images, it is recommended that you use "Save for Web" command with the following settings:

File Format: PNG-8, non-interlaced
Color Reduction: Selective, 256 colors
Dithering: Diffusion, 88%
Transparency: On, Matte: None
Transparency Dither: Diffusion Transparency Dither, 100%

<小时>

为了其他人的利益,这是我有效的水印代码:


And for other's benefits, this is the watermark code I have that works:

<?
$masterURL = 'mydomain.com/myImage.jpg';

header('content-type: image/jpeg');
$watermark = imagecreatefrompng('watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($masterURL);
$size = getimagesize($masterURL);
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>

阅读更多内容后(此评论 同一篇文章),我发现您可以使用 PNG-24 水印,但使用 imagecopy 而不是 imagecopymerge.您可以替换此行:

After a bit more reading (this comment on the same article), I found out that you CAN use PNG-24 watermarks but with imagecopy instead of imagecopymerge. You can replace this line:

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);

有了这个:

imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);

使用带有 PNG-8 水印的 imagecopymerge 非常适合即时"水印,因为水印文件很小.如果您在幕后"添加水印,则文件大小并不重要,您可以使用 imagecopy 从 PNG-24 水印中获得更好的质量.

Using imagecopymerge with PNG-8 watermarks is quite good for 'on-the-fly' watermarking, as the watermark file is tiny. If you do watermarking 'behind-the-scenes', it doesn't really matter about file size and you can get much better quality from the PNG-24 watermark, using imagecopy.

我希望这能帮助那些困惑的水印.

I hope this helps the confused watermarkers out there.

这篇关于PHP - 水印 PNG 透明度/Alpha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆