PHP GD 使用一张图像遮盖另一张图像,包括透明度
我正在尝试创建一个获取图像的 PHP 脚本:
https://i.sstatic.net/eNvlM.png
然后应用 PNG 图像:
< img src="https://i.sstatic.net/GYHwi.png" alt="掩码">
https://i.sstatic.net/iJr2I.png
作为掩码。
最终结果需要保持透明度:
https://i.sstatic.net/u0l0I.png
如果可能的话我想做在 GD 中,ImageMagick 目前还不是一个真正的选择。
我该怎么办呢?
phalacee的帖子(在“PHP/GD,如何将一个圆从一个图像复制到另一个图像?”)似乎是正确的,但我特别需要使用图像作为蒙版,不是形状。
I am trying to create a PHP script that takes an image:
https://i.sstatic.net/eNvlM.png
and then applies a PNG image:
https://i.sstatic.net/iJr2I.png
as a mask.
The end result needs to maintain transparency:
https://i.sstatic.net/u0l0I.png
If at all possible I want to do this in GD, ImageMagick is not really an option right now.
How would I go about this?
phalacee's post (in "PHP/GD, how to copy a circle from one image to another?") seems to be along the right lines but I specifically need to use an image as a mask, not a shape.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
马特,
如果您使用黑色背景上的椭圆形白色填充而不是透明背景上的黑色填充来制作 png,则以下函数可以做到这一点。
Matt,
If you make your png with the oval white fill on black background instead of black fill with transparent background the following function does it.
这是对该脚本的一点升级 - 我发现如果源图像本身具有透明度,则蒙版(使用上面的脚本)会绘制后像素而不是源图像的透明像素。下面的扩展脚本考虑了源图像透明度并保留它。
Here's a little upgrade to this script - I found that if the source image has transparency itself, the mask (using the script above) plots a back pixel instead of the source image's transparent pixel. The below extended script takes source image transparency into consideration, and preserves it.
我喜欢你的脚本,当像素完全透明时删除额外的颜色信息是个好主意。如果有人想使用这种方法,我应该指出一个小错误(IMO)。
也应该是,
如果像素是 100% 透明,我也不是 100% 确定为什么要在此处检查 RGB 值
,并且我不确定遮罩文件中的 alpha 混合是否适合您的方法,因为它仅在 rgba 时使用值全部等于 0。
Jules 的脚本也非常好,尽管它期望蒙版是蒙版的灰度表示(这是很常见的做法)。
在 Matt 的查询中,他需要一个脚本,该脚本仅从现有图像中获取 Alpha 透明度并将其应用到另一个图像。这是 Jules 脚本的一个简单模型,只是为了从蒙版图像中获取 Alpha,并保留源图像的 Alpha。
I like your script, good idea to remove extra colour information when the pixel is totally transparent. I should point out just a small error (IMO) though if anyone wants to use this method.
should be
also I'm not 100% sure why you're checking rgb values here if the pixel is 100% transparent
and I'm not sure alpha blending from the mask file would work well with your method, as its only used when rgba values all equal 0.
Jules's script is pretty good too, though it expects the mask to be a grey scale representation of a mask (which is pretty common practice).
In Matt's query he was after a script that grabs just the alpha transparency from an existing image and applies it to another image. Here's a simple mod of Jules's script just to grab the alpha from the mask image, and preserve the alpha of the source image.
有一个名为 WideImage 的库,支持 alpha 遮罩 http://wideimage.sourceforge.net/documentation/manipulated -图像/
There's a library called WideImage that supports alpha masks http://wideimage.sourceforge.net/documentation/manipulating-images/
这里是对第一个功能的一点升级。由于您已经有了透明图像,因此无需复制遮罩像素。这会对执行有一点帮助。
Here is a little upgrade for the first function. As you already have a transparent image, you don't need to copy the masked pixels. This will help the execution a little.
获得类似效果的另一种方法是将 png 文件粘贴到具有唯一背景颜色的新图像上以暂时删除透明度,并将 png 图像的透明颜色设置为黑色圆圈颜色。然后,当您将其放置在 jpeg 图像上时,将新的透明颜色设置为蒙版的颜色。
如果重新采样或半透明像素让您感到沮丧,您可以禁用混合并在
$mask
图像上绘制透明形状,而不是使用 png 作为遮罩。注意:上面的代码未经测试,但应该可以满足您的需要。
A different way to get a similar effect is to paste the png file onto a new image with a unique background colour to temporarily remove transparency, and set the transparent colour of the png image to the black circle colour instead. Then when you place it over the jpeg image, you set the new transparent colour to the colour of the mask.
If resampling or semi-transparent pixels are getting you down, instead of using a png as a mask, you can disable blending and draw a transparent shape onto
$mask
image instead.Note: The above code is untested, but should hopefully do what you need it to.