我需要创建的是一个动态的多色渐变图像,如下所示:
我需要为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),0); // Line 3: red = 0 ; green = 255 ; blue = 0 -> 255 $mycolors[$i + $limit*2] = imagecolorallocate($im,$i); // Line 4: red = 0 ; green = 255 -> 0 ; blue = 255 $mycolors[$i + $limit*3] = imagecolorallocate($im,255); // Line 5: red = 0 -> 255 ; green = 0 ; blue = 255 $mycolors[$i + $limit*4] = imagecolorallocate($im,255); // Line 6: red = 255 ; green = 0 ; blue = 255 -> 0 $mycolors[$i + $limit*5] = imagecolorallocate($im,(255 - $i)); } for ($i=0; $i < $size; $i++) { imageline($im,$thickness-1,$mycolors[$i]); } imagepng($im); imagegd($im); imagedestroy($im);
我在this article的帮助下创建了上面的图像
但是这里的代码是使用静态颜色代码,当我试图用它操纵时,我得到的图像如下:
编辑代码
$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,($r1 - $i),($g3 - $i),$b4); // Line 6: red = 255 ; green = 0 ; blue = 255 -> 0 $mycolors[$i + $limit*5] = imagecolorallocate($im,$r5,($b5 - $i)); } for ($i=0; $i < $size; $i++) { imageline($im,$mycolors[$i]); } imagepng($im); imagegd($im); imagedestroy($im);
您可以看到渐变效果丢失,因为静态255被动态值替换,动态值创建负数,从而失去其效果.
任何人都可以帮助我使用此代码创建动态颜色渐变图像,或者可以帮助我为gd库实现此类输出提供另一个方向.
更新
现在当我得到这个问题的答案时,我遇到了一个与这个问题相关的新问题.
我想要实现的是现在的随机颜色分配,如下所示:
我将使用六色阵列,如上面的问题,需要一些提示才能开始.
你自己已经找到了问题,实际上……让我们进一步研究:
You can see that the gradient effect is lost,because the static 255 is replaced with dynamic value which is creating negative numbers and thus losing its effect.
因此,不是每次都以$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 }