1

好吧,几个月来我一直在研究 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 人吗?
更新:这个问题为我赢得了风滚草徽章。

4

1 回答 1

1

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

使用 Kohana 图像库的用法如下:

// 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)(和imagesyfor $this->height)。$this->_image指从现有图像创建的 GD 资源。

Oh, and make sure you render the image as a PNG or the gradient alpha will not work properly... :)

于 2009-07-15T20:36:58.540 回答