2009-07-07 90 views
1

好的,我一直在用GD Image进行几个月的黑客攻击,我想完成的一项任务是创建一个脚本,它接收现有图像,然后创建一种反射在它下面变得透亮。
以下指南显示了如何用不透明的颜色做:TalkPHP Forums Link

在该论坛上,Rendair描述了一种使用颜色,用下面的PHP代码,以覆盖一个动态绘制渐变:PHP中的半透明图像反射GD

// 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的人谁有任何天才的想法?
更新:这个问题为我赢得了风滚草徽章。

+0

这是一个非常酷的想法,我_think_我有这方面的解决方案...我会测试一些代码并回来。 – shadowhand 2009-07-15 16:15:12

+0

哦,我应该问过...你想把图像的高度加倍(上面的标准图像,下面翻转和褪色的反射)? – shadowhand 2009-07-15 16:17:51

回答

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)(并且为imagesy)。 $this->_image是指从现有图像创建的GD资源。

哦,并确保您呈现图像为PNG或渐变阿尔法将无法正常工作... :)