0

私はキャプチャ画像タイプのスクリプトに取り組んでおり、画像の各文字の色を交互に変えたいと思っています。これまでのところ、色の値が期待したものではないことを除いて、ほとんど機能しています

この $multi_text_color 変数の色は、ランダムに選択して表示する色である必要があります。カラー配列の値をランダムに選択することはできますが、画像に配置されている色は 3 色であり、私が望むものに近くありません。ここで何か間違ったことをしていますか?

<?PHP
// note this is just the part I am having trouble with, I have taken everything else out that adds line and tilts the letters and stuff so just the color settings are in this bit

// My this part of my script takes each number/letter in a string and makes it a random color to put on the image

// set color values
$multi_text_color = "#FF3E96,#6A5ACD,#90EE90";
// put colors above into an array
$colors = explode(',', $multi_text_color);
// cycle through everything to add the letters/numbers to image
for($i = 0; $i < $characters; ++$i) {
    $idx = rand(0, 2);
    // notice my $colors variable has random number for the color array
    $r = substr($colors[$idx], 1, 2); // shows: f6 or 8d or FF
    $g = substr($colors[$idx], 3, 2); // shows: 3E or 32 or 5c
    $b = substr($colors[$idx], 5, 2); // shows: 96 or 47 or fd 
    $font_color = imagecolorallocate($image, "$r", "$g", "$b");
    // finish it up
    imagettftext($image, $font_size, $angle, $x, $y, $font_color, $this->font, $code{$i});
}
?>
4

2 に答える 2

1

imagecolorallocate() は、文字列ではなく整数をパラメーターとして受け取ります。最初に hexdec() を使用して、$r、$g、および $b を整数に変換します。

于 2009-08-01T05:18:06.967 に答える
0

16 進数を使用している場合は、最初に 10 進数に変換する必要があります。

$font_color = imagecolorallocate($image, hexdec($r), hexdec($g), hexdec($b));
于 2009-08-01T05:28:28.867 に答える