Несколько градусов цвет в PHP GD - PullRequest
1 голос
/ 11 марта 2020

Я пытаюсь сделать градусное цветное изображение в библиотеке 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 );

Надеюсь, вы можете мне помочь,

Большое спасибо.

1 Ответ

0 голосов
/ 11 марта 2020

Ну, я уже решил вопрос. Я забыл вычесть значение $ stop в color:

$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 ) ) ) );

Это делает апропиатное вычисление цвета. Большое спасибо всем.

...