PHP 中具有不透明度的径向渐变
我需要创建一个具有不透明度的 PNG 径向渐变。我浏览过 GDLib 但我看不到生成径向渐变的方法。有谁知道使用 GDlib 或任何其他 PHP 图形库的方法吗?
我想最坏的情况我可以使用 GDLib 逐像素生成它,但是如何开始对此进行数学计算呢?
目标是为网页生成性感的灯光效果背景 PNG。可以在 标题 上看到该效果的示例,它使用 此背景图片。我尝试过通用的白色照明效果 PNG,但它看起来不如有色照明,所以我生成的 PNG 将考虑网站的配色方案。
我认为服务器端是可行的方法,因为浏览器对 CSS 径向渐变 的支持非常不完整。
I need to create a PNG radial gradient with opacity. I've looked through GDLib but I can't see a way to generate radial gradients. Does anyone know of a way with GDlib or any other graphics library for PHP?
I suppose worst-case I could generate it pixel-by-pixel using GDLib but how does one even start to do the math on that?
The goal is to generate sexy lighting effect background PNGs for web pages. An example of the effect can be seen on the header here which uses this background image. I've tried generic white lighting effect PNGs but it doesn't look anywhere near as good as tinted lighting, so my generated PNGs will take into account the website's color scheme.
I assume server-side is the way to go because browser support for CSS radial gradients is so patchy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不结合使用 imagecolorallocatealpha() 和 imageellipse() 或 imagefilledellipse()?
编辑:
请参阅此类了解相关示例我是说。您应该能够扩展它以支持 alpha。
Edit2:
我对上面的类做了一些修改以产生 alpha 支持。它并不完美,但它适用于省略号等:
http://codepad.org/1eZ3Km0J
Why not use a combination of imagecolorallocatealpha() and imageellipse() or imagefilledellipse()?
Edit:
See this class for an example of what I mean. You should be able to extend this to support alpha.
Edit2:
I have made some modifications to the class above to yield alpha support. It's not perfect, but it works for ellipses and such:
http://codepad.org/1eZ3Km0J
经典的视频游戏技巧是应用渐变纹理,而不是计算光线。这是该技术的完美用途。
在较大的像素尺寸(2048px 方形很常见)上制作灰度渐变,并选择几个较小的像素尺寸(1024,512,256px 等)根据您的需要选择最接近的一个(放大可能会夸大条带,缩小可能会引入莫尔条纹)。
使用 php gd 函数,例如
imagecopymerge
。根据意图,您可以在第一次使用时存储结果。the classic video game trick is to apply a gradient texture, rather than compute the light. this is a perfect use for the technique.
make a grayscale gradient at a large-ish pixel dimension (2048px square is common) and several smaller ones (1024,512,256px etc) pick the closest one for your need (scaling up may exaggerate banding, scaling down may introduce moire).
use php gd function such as
imagecopymerge
. depending on intent, you could store the result on first use.数学很简单,
alpha = max_alpha - (distance_to_center / radius)
,其中距离是欧几里得距离,即sqrt( (x1-x2)^2 + (y1-y2)^2 )< /代码>。
The math is easy,
alpha = max_alpha - (distance_to_center / radius)
where the distance is Euclidean, i.e.sqrt( (x1-x2)^2 + (y1-y2)^2 )
.