Implementation Code:
<? Php
// Save the verification code to the session for global use
Session_start ();
$ Nums = "";
For ($ I = 0; $ I <4; $ I ++ ){
// Generate a random number and convert it to hexadecimal notation
$ Nums. = dechex (mt_rand (0, 15 ));
}
// Write the verification code to the session
$ _ SESSION ['code'] = $ nums;
// Set the length and width of the Verification Code
$ _ Width = 60;
$ _ Height = 20;
// Create an image
$ _ Img = imagecreatetruecolor ($ _ width, $ _ height );
// Create a white
$ _ White = imagecolorallocate ($ _ img, 220,250,250 );
// Fill in the background
Imagefill ($ _ img, 0, 0, $ _ white );
// Draw 6 lines randomly
For ($ I = 0; $ I <6; $ I ++ ){
$ _ Rnd_color = imagecolorallocate ($ _ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imageline ($ _ img, mt_rand (0, $ _ width), mt_rand (0, $ _ width), mt_rand (0, $ _ width), mt_rand (0, $ _ width), $ _ rnd_color );
}
// Randomly plot the snowflake
For ($ I = 0; $ I <60; $ I ++ ){
Imagestring ($ _ img, 1, mt_rand (1, $ _ width), mt_rand (1, $ _ height), "*", imagecolorallocate ($ _ img, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 )));
}
// Output the verification code
For ($ I = 0; $ I <strlen ($ _ SESSION ['code']); $ I ++ ){
Imagestring ($ _ img, mt_rand (6, 10), $ I * $ _ width/4 + mt_rand (1, 10), mt_rand (1, $ _ height/2 ), $ _ SESSION ['code'] [$ I], imagecolorallocate ($ _ img, mt_rand (0,100), mt_rand (0,150), mt_rand (0,200 )));
}
// Output and destroy
Header ("Content-Type: image/png ");
Imagepng ($ _ img );
Imagedestroy ($ _ img );
?>