动态多色渐变图像

时间:2015-05-20 10:19:54

标签: php image gd gradient

我需要创建的是一个动态的多色渐变图像,如下所示:

enter image description here

我需要为2/3/4/5/6颜色动态创建它,现在我正在使用6色渐变。

到目前为止我所做的是:

$size = 1536;
$thickness = 54;
$im = imagecreatetruecolor($size, $thickness);

$clrCount = count($clr);            
$limit = floor($size/$clrCount);

        for($i = 0; $i < $limit; $i++) {

            // Line 1: red = 255 ; green = 0 -> 255 ; blue = 0
            $mycolors[$i] = imagecolorallocate($im, 255, $i, 0);
            // Line 2: red = 255 -> 0 ; green = 255 ; blue = 0
            $mycolors[$i + $limit] = imagecolorallocate($im, (255 - $i), 255, 0);
            // Line 3: red = 0 ; green = 255 ; blue = 0 -> 255
            $mycolors[$i + $limit*2] = imagecolorallocate($im, 0, 255, $i);
            // Line 4: red = 0 ; green = 255 -> 0 ; blue = 255
            $mycolors[$i + $limit*3] = imagecolorallocate($im, 0, (255 - $i), 255);
            // Line 5: red = 0 -> 255 ; green = 0 ; blue = 255
            $mycolors[$i + $limit*4] = imagecolorallocate($im, $i, 0, 255);
            // Line 6: red = 255 ; green = 0 ; blue = 255 -> 0
            $mycolors[$i + $limit*5] = imagecolorallocate($im, 255, 0, (255 - $i));


        }

        for ($i=0; $i < $size; $i++) { 
            imageline($im, $i, 0, $i, $thickness-1, $mycolors[$i]);
        }

imagepng($im);
imagegd($im);
imagedestroy($im);

我在this article

的帮助下创建了上面的图片

但是这里的代码是使用静态颜色代码,当我尝试用它操作时,我得到的图像如下:

enter image description here

已编辑的代码

$size = 1536;
$thickness = 54;
$im = imagecreatetruecolor($size, $thickness);

$clrCount = count($clr);            
$limit = floor($size/$clrCount);

$clr = array(0 => '4d6eae', 1 => 'e58f0e', 2 => 'ff00ff', 3 => '9900ff', 4 => '9f560a', 5 => '93c47d');

        list($r, $g, $b) = sscanf($clr[0], "%02x%02x%02x");
        list($r1, $g1, $b1) = sscanf($clr[1], "%02x%02x%02x");
        list($r2, $g2, $b2) = sscanf($clr[2], "%02x%02x%02x");
        list($r3, $g3, $b3) = sscanf($clr[3], "%02x%02x%02x");
        list($r4, $g4, $b4) = sscanf($clr[4], "%02x%02x%02x");
        list($r5, $g5, $b5) = sscanf($clr[5], "%02x%02x%02x");

        for($i = 0; $i < $limit; $i++) {

            // Line 1: red = 255 ; green = 0 -> 255 ; blue = 0
            $mycolors[$i] = imagecolorallocate($im, $r, $i, 0);
            // Line 2: red = 255 -> 0 ; green = 255 ; blue = 0
            $mycolors[$i + $limit] = imagecolorallocate($im, ($r1 - $i), $g1, 0);
            // Line 3: red = 0 ; green = 255 ; blue = 0 -> 255
            $mycolors[$i + $limit*2] = imagecolorallocate($im, 0, $g2, $i);
            // Line 4: red = 0 ; green = 255 -> 0 ; blue = 255
            $mycolors[$i + $limit*3] = imagecolorallocate($im, 0, ($g3 - $i), 255);
            // Line 5: red = 0 -> 255 ; green = 0 ; blue = 255
            $mycolors[$i + $limit*4] = imagecolorallocate($im, $i, 0, $b4);
            // Line 6: red = 255 ; green = 0 ; blue = 255 -> 0
            $mycolors[$i + $limit*5] = imagecolorallocate($im, $r5, 0, ($b5 - $i));


        }

        for ($i=0; $i < $size; $i++) { 
            imageline($im, $i, 0, $i, $thickness-1, $mycolors[$i]);
        }

imagepng($im);
imagegd($im);
imagedestroy($im);

您可以看到渐变效果丢失,因为静态255被动态值替换,动态值会产生负数,从而失去效果。

任何人都可以帮助我使用此代码创建动态颜色渐变图像,或者可以帮助我为gd库实现此类输出提供另一个方向。

已更新

现在当我得到这个问题的答案时,我遇到了与这个问题相关的新问题。

我想要实现的是随机颜色分配,如下所示:

enter image description here

我正在使用六色阵列,如上面的问题一样,需要一些提示才能开始。

1 个答案:

答案 0 :(得分:3)

你自己已经找到了问题,实际上......让我们继续努力:

  

您可以看到渐变效果丢失,因为静态255被动态值替换,动态值会产生负数,从而失去效果。

因此,您不必每次都使用$i减少,而是以$i的百分比减少。

<强>伪代码

我们想要从红色值:50到红色值:75步中200。 我们怎么做?

$red = 50;
for ($i = 0; $i < 75; $i++) {
    $red = 50 + ((200 - 50) / 75) * $i;
    //Our color is: 
         // The difference between the limits (150: 200-50)
         // divided by the amount of steps: 75.
         // We multiply the result by the current step.
         // And we add the lower limit to it, so we start at 50.
}

此代码将为我们提供:

50 - 52 - 54 - 56 - 58 - ... - 192 - 194 - 196 - 198

现在,让我们调整您的代码以反映这一点:

for($i = 0; $i < $limit; $i++) {
    $mycolors[$i] = imagecolorallocate($im, 
        $r + (($r1 - $r) / $limit) * $i,
        $g + (($g1 - $g) / $limit) * $i,
        $b + (($b1 - $b) / $limit) * $i);

     $mycolors[$i + $limit] = imagecolorallocate($im, 
        $r1 + (($r2 - $r1) / $limit) * $i,
        $g1 + (($g2 - $g1) / $limit) * $i,
        $b1 + (($b2 - $b1) / $limit) * $i);

     //Copy-paste magic here
}