Radial gradients with opacity in PHP

对着背影说爱祢 提交于 2019-12-10 09:52:00

问题


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.


回答1:


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




回答2:


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.




回答3:


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 math is easy, alpha = max_alpha - (distance_to_center / radius) where the distance is Euclidean, i.e. sqrt( (x1-x2)^2 + (y1-y2)^2 ).



来源:https://stackoverflow.com/questions/6615602/radial-gradients-with-opacity-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!