You may have seen the verification code. in this article, we will introduce how PHP implements the color-changing verification code. if you are interested, refer
The code is as follows:
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 = 'This verification code is too abnormal ';
$ Textlen = iconv_strlen ($ text, 'utf-8'); // calculates the string length.
// Randomly intercepts two characters for color display
$ P1 = rand (1, $ textlen)-1;
While ($ p2 = rand (1, $ textlen)-1) = $ p1 );
$ W1 = iconv_substr ($ text, $ p1, 1, 'utf-8 ');
$ W2 = iconv_substr ($ text, $ p1, 1, 'utf-8 ');
// Font file (PS: T good php Q buckle: 276167802, verification: csl)
$ Font = 'simkai. ttf ';
Imagefilledrectangle ($ im, 0, 0,399, 29, $ white );
For ($ I = 0; $ I <$ textlen; $ I ++)
{
If ($ I = $ p1 | $ I = $ p2)
{
Imagettftext ($ im, 15, 0, 20 * ($ i-1) + 20, 20, $ color_arr [$ color], $ font, iconv_substr ($ text, $ I, 1, 'utf-8 '));
}
Else
{
Imagettftext ($ im, 15, 0, 20 * ($ i-1) + 20, 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, so that users can enter the verification code of the specified color, which is more secure.