【问题标题】:Multiple degree color in PHP GDPHP GD中的多度颜色
【发布时间】:2020-03-11 10:06:45
【问题描述】:

我正在尝试在 PHP GD 库中制作度数彩色图像。 我刚刚编写了一个简单的代码,可以很好地处理 2 种颜色,但是,如果我输入的颜色超过 2 种,就会变得很奇怪。

我的代码:

function gradientfilledrectangle ( $image, $x1, $y1, $x2, $y2, $colors ) {

    if ( $x1 > $x2 || $y1 > $y2 ) { return 0; }

    $steps = $y2 - $y1;
    $s = 0;
    $stop = 0;

    $st = $steps / ( count ( $colors ) - 1 );

    for ( $i = 0; $i < ( count ( $colors ) - 1 ); $i++ ) {

        for ( ; $s < ( $st + $stop ); $s++ ) {

            $r = abs ( round ( $colors [$i] [0] - ( ( ( $colors [$i] [0] - $colors [$i + 1] [0] ) / $st ) * $s ) ) );
            $g = abs ( round ( $colors [$i] [1] - ( ( ( $colors [$i] [1] - $colors [$i + 1] [1] ) / $st ) * $s ) ) );
            $b = abs ( round ( $colors [$i] [2] - ( ( ( $colors [$i] [2] - $colors [$i + 1] [2] ) / $st ) * $s ) ) );

            $color = imagecolorallocate ( $image, $r, $g, $b );

            imagefilledrectangle ( $image, $x1, $y1 + $s, $x2, $y1 + $s + 1, $color );

        }

        $stop += $st;

    }

    return 1;

}

然后调用:

list ( $w, $h ) = array ( 600, 600 );

$im = imagecreatetruecolor ( $w, $h );

$degree = array (
    array ( 200, 100, 0 ),
    array ( 0, 100, 200 ),
    array ( 100, 200, 0 )
);

if ( count ( $degree ) < 2 ) {

    imagefilledrectangle ( $im, 0, 0, $w, $h, imagecolorallocate ( $im, $degree [0] [0], $degree [0] [1], $degree [0] [2] ) );

} else {

    gradientfilledrectangle ( $im, 0, 0, $w, $h, $degree );

}

header ( 'Content-Type: image/jpeg' );

imagejpeg ( $im, null, 100 );
imagedestroy ( $im );

希望你能帮助我,

非常感谢。

【问题讨论】:

    标签: php php-gd


    【解决方案1】:

    好吧,我已经解决了这个问题。 我忘记将 $stop 值减去颜色:

    $r = abs ( round ( $colors [$i] [0] - ( ( ( $colors [$i] [0] - $colors [$i + 1] [0] ) / $st ) * ( $s - $stop ) ) ) );
    $g = abs ( round ( $colors [$i] [1] - ( ( ( $colors [$i] [1] - $colors [$i + 1] [1] ) / $st ) * ( $s - $stop ) ) ) );
    $b = abs ( round ( $colors [$i] [2] - ( ( ( $colors [$i] [2] - $colors [$i + 1] [2] ) / $st ) * ( $s - $stop ) ) ) );
    

    这使得颜色的适当计算。 非常感谢大家。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-06
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多