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

如果您使用的是十六进制数字,则需要先将它们转换为小数。

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