// Image. php
<? PHP
Ob_clean ();
Header ("Content-Type: image/GIF ");
/*
* Initialization
*/
$ Border = 0; // whether the border is required; 1: 0: not required
$ How = 4; // number of digits of the Verification Code
$ W = $ how * 25; // Image Width
$ H = 45; // Image Height
$ Fontsize = 15; // font size
$ Alpha = "abcdefghijkmnopqrstuvwxyz"; // Verification Code content 1: letter
$ Number = "023456789"; // Verification Code content 2: Number
$ Randcode = ""; // verification code string Initialization
Srand (double) microtime () * 1000000); // initialize the random number Seed
$ Im = imagecreate ($ W, $ H); // create a verification Image
/*
* Draw the Basic Framework
*/
$ Bgcolor = imagecolorallocate ($ im, 255,255,255); // you can specify the background color.
Imagefill ($ im, 0, 0, $ bgcolor); // fill the background color
If ($ Border)
{
$ Black = imagecolorallocate ($ im, 0, 0, 0); // you can specify the border color.
Imagerectangle ($ im, 0, 0, $ W-1, $ h-1, $ black); // draw a border
}
/*
* Generate random characters by bit
*/
For ($ I = 0; $ I <$ how; $ I ++)
{
$ Alpha_or_number = mt_rand (0, 1); // letters or numbers
$ STR = $ alpha_or_number? $ ALPHA: $ number;
$ Which = mt_rand (0, strlen ($ Str)-1); // specify the character
$ Code = substr ($ STR, $ which, 1); // obtain the character
$ J =! $ I? 4: $ J + 25; // specifies the position of the painted character.
$ Color3 =
Imagecolorallocate ($ im, mt_rand (0,100), mt_rand (0,100), mt_rand (0,100); // the color of the character.
Imagechar ($ im, $ fontsize, $ J, 16, $ code, $ color3); // draw characters
$ Randcode. = $ code; // Add the verification code to the code bit by bit
}
/*
* Add interference
*/
For ($ I = 0; $ I <5; $ I ++) // draws the background interference line
{
$ Color1 = imagecolorallocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255); // interference line color
Imagearc ($ im, mt_rand (-5, $ W), mt_rand (-5, $ H), mt_rand (20,300), mt_rand (20,200), 55, 44, $ color1); // interference line
}
For ($ I = 0; $ I <$ how * 40; $ I ++) // draws background interference points
{
$ Color2 = imagecolorallocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255); // interference point color
Imagesetpixel ($ im, mt_rand (0, $ W), mt_rand (0, $ H), $ color2); // interference point
}
// Write the verification code string to the session
Session_start ();
$ _ Session ['randcode'] = $ randcode;
/* Painting
Graph end */
Imagegif ($ IM );
Imagedestroy ($ IM );
/* Drawing ends */
?>