Copy Code code as follows:
<?php
Header ("content-type:image/png,charset= ' Utf-8 '");
$im = Imagecreatetruecolor (400, 30);
White
$white = Imagecolorallocate ($im, 255, 255, 255);
Red
$red = Imagecolorallocate ($im, 255, 0, 0);
Black
$black =imagecolorallocate ($im, 0, 0, 0);
Green
$green =imagecolorallocate ($im, 0, 255, 0);
Blue
$blue =imagecolorallocate ($im, 0, 0, 255);
$color _arr=array ($green, $blue, $red);
$color =array_rand ($color _arr);
$text = ' I am so sick of this verification code ';
$textlen =iconv_strlen ($text, ' utf-8 ');//Calculate string length
Randomly intercept two characters, color display
$p 1=rand (1, $textlen)-1;
while (($p 2=rand (1, $textlen)-1) = = $p 1);
$w 1=iconv_substr ($text, $p 1,1, ' utf-8 ');
$w 2=iconv_substr ($text, $p 1,1, ' utf-8 ');
Font files (ps:t good PHP Q-buckle 峮: 276167802, Validation: CSL)
$font = ' Simkai.ttf ';
Imagefilledrectangle ($im, 0, 0, 399, $white);
for ($i =0; $i < $textlen; $i + +)
{
if ($i = = $p 1| | $i = = $p 2)
{
Imagettftext ($im, 0, 20* ($i-1) +20, $color _arr[$color], $font, Iconv_substr ($text, $i, 1, ' utf-8 '));
}
Else
{
Imagettftext ($im, 0, 20* ($i-1) +20, $black, $font, Iconv_substr ($text, $i, 1, ' utf-8 '));
}
}
Imagepng ($im);
Imagedestroy ($im);
?>
The characters in the verification code are not the same color, allowing the user to enter the validation code for the specified color, which is more secure.