PHP GD 中的半透明图像反射
好吧,我已经使用 GD Image 几个月了,我想用它完成的一项任务是创建一个脚本,该脚本采用现有图像,然后创建一个在其下方淡出为半透明的反射。
以下指南展示了如何使用不透明颜色: TalkPHP 论坛链接
在该论坛中,Rendair 描述了一种使用颜色覆盖动态绘制渐变的方法,使用以下 PHP 代码:
// Next we draw a GD line into our gradient_line
imageline ($gradient_line, 0, 0, $imgName_w, 0, $gdGradientColor);
$i = 0;
$transparency = 30; //from 0 - 100
while ($i < $gradientHeight) //create line by line changing as we go
{
imagecopymerge ($background, $gradient_line, 0,$gradient_y_startpoint, 0, 0, $imgName_w, 1, $transparency);
++$i;
++$gradient_y_startpoint;
if ($transparency == 100) {
$transparency = 100;
}
else
{
// this will determing the height of the
//reflection. The higher the number, the smaller the reflection.
//1 being the lowest(highest reflection)
$transparency = $transparency + 1;
}
}
我试图实现的是一种效果,我们使用 alpha 功能将每条线类似地淡入更半透明的阴影,但似乎我很难一次一行地应用它。 到目前为止,我只能制作图像的一小部分(一行大),然后用半透明覆盖它,我似乎无法使每行稍微褪色一点。 所以我的预期结果应该是初始图像,然后是渐变到 100% Alpha 透明的反射副本,但我似乎无法实现这一点。
那里有任何 PHP 人员有什么天才的想法吗?
更新:这个问题为我赢得了风滚草徽章。
Alright, I've been hacking around with GD Image for a couple of months, and a task that I wanted to accomplish with it was to create a script that takes an existing image, and then creates a reflection that fades out to translucent beneath it.
The following guide shows how to do it with an opaque color : TalkPHP Forums Link
In that forum, Rendair describes a way to overlay a dynamically drawn gradient using a color, with the following PHP code:
// Next we draw a GD line into our gradient_line
imageline ($gradient_line, 0, 0, $imgName_w, 0, $gdGradientColor);
$i = 0;
$transparency = 30; //from 0 - 100
while ($i < $gradientHeight) //create line by line changing as we go
{
imagecopymerge ($background, $gradient_line, 0,$gradient_y_startpoint, 0, 0, $imgName_w, 1, $transparency);
++$i;
++$gradient_y_startpoint;
if ($transparency == 100) {
$transparency = 100;
}
else
{
// this will determing the height of the
//reflection. The higher the number, the smaller the reflection.
//1 being the lowest(highest reflection)
$transparency = $transparency + 1;
}
}
What I'm attempting to accomplish is an effect where we similarly fade each line to one shade more translucent using the alpha feature, but it seems that I have a hard time applying it one line at a time. So far I can only make a tiny slice of the image (one line large) and then overlay it with translucency, I can't seem to fade each line a bit more. So my intended result should be the initial image, then a reflected copy that fades to 100% alpha transparent, but I can't seem to accomplish this.
Any PHP folks out there who have any genius ideas?
Update: this question has earned me the tumbleweed badge.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,那很激烈。 长话短说, imagecopymerge 无法正确处理 Alpha 通道。 相反,您需要将 imagefilter 与
IMG_FILTER_COLORIZE
滤镜结合使用来降低每个图像的不透明度线。 此代码现在是 Image_GD(BSD 许可证)。 我试图使代码尽可能清晰,但如果您有任何疑问,请告诉我。Kohana Image 库的用法如下:
真正重要的位是第 265-287 行,它处理实际的逐行渐变创建。
$this->width
的所有实例都可以转换为imagesx($image)
(对于$this->width 则转换为
imagesy
)。高度)。$this->_image
指的是从现有图像创建的 GD 资源。哦,请确保将图像渲染为 PNG,否则渐变 alpha 将无法正常工作...:)
All right, that was intense. To keep a long story short, imagecopymerge does not properly handle alpha channels. Instead, you need to use imagefilter with the
IMG_FILTER_COLORIZE
filter to lower the opacity of each line. This code is now part of Image_GD (BSD license). I tried to make the code as clear as possible, but let me know if you have any questions.Usage using the Kohana Image library works like this:
The really important bits are lines 265-287, which handles the actual line-by-line gradient creation. All instances of
$this->width
can translate intoimagesx($image)
(andimagesy
for$this->height
).$this->_image
refers to an GD resource that was created from an existing image.Oh, and make sure you render the image as a PNG or the gradient alpha will not work properly... :)