PHP GD中的半透明图像反射

时间:2009-07-07 14:52:09

标签: php reflection gd

好吧,我几个月来一直在使用GD Image进行攻击,我想用它完成的任务是创建一个采用现有图像的脚本,然后创建一个渐弱的反射半透明的。
以下指南介绍了如何使用不透明的颜色:TalkPHP Forums Link

在该论坛中,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人员? 更新:这个问题为我赢得了风滚草徽章。

1 个答案:

答案 0 :(得分:1)

好吧,那是激烈的。长话短说,imagecopymerge无法正确处理Alpha通道。相反,您需要将imagefilterIMG_FILTER_COLORIZE过滤器一起使用,以降低每行的不透明度。此代码现在是Image_GD(BSD许可证)的一部分。我尽量让代码尽可能清晰,但如果您有任何问题,请告诉我。

使用Kohana Image库的用法如下:

// Makes a 20px tall reflection with a starting opacity of 100%
// and overwrites the original image with the new one
Image::factory($image_file)->reflection(20, 100)->save();

真正重要的位是265-287行,它处理实际的逐行梯度创建。 $this->width的所有实例都可以转换为imagesx($image)(以及imagesy $this->height)。 $this->_image是指从现有图像创建的GD资源。

哦,并确保将图像渲染为PNG,否则渐变alpha将无法正常工作......:)