The Code is as follows: <? Php /* Website Verification Code Program * Running environment: PHP5.0.18 is successfully debugged. * Gd2 graphics library is required (php_gd2.dll is enabled in PHP. INI) * File name: showimg. php * Author: 17php.com * Time: 2007.03 */ // Generate a 4-digit Verification Code randomly $ Num = ""; For ($ I = 0; $ I <4; $ I ++ ){ $ Num. = rand (0, 9 ); } // The four-digit verification code can also be directly generated using rand (,) // Write the generated verification code to the session, which is used on the standby verification page. Session_start (); $ _ SESSION ["Checknum"] = $ num; // Create an image and define the color value Header ("Content-type: image/PNG "); Srand (double) microtime () * 1000000 ); $ Im = imagecreate (60, 20 ); $ Black = ImageColorAllocate ($ im, 0, 0 ); $ Gray = ImageColorAllocate ($ im, 200,200,200 ); Imagefill ($ im, 0, 0, $ gray ); // Randomly draw two dotted lines to interfere $ Style = array ($ black, $ gray, $ gray ); Imagesetstyle ($ im, $ style ); $ Y1 = rand (0, 20 ); $ Y2 = rand (0, 20 ); $ Y3 = rand (0, 20 ); $ Y4 = rand (0, 20 ); Imageline ($ im, 0, $ y1, 60, $ y3, IMG_COLOR_STYLED ); Imageline ($ im, 0, $ y2, 60, $ y4, IMG_COLOR_STYLED ); // Randomly generate a large number of black spots on the canvas, which can interfere with the canvas; For ($ I = 0; $ I <80; $ I ++) { Imagesetpixel ($ im, rand (0, 60), rand (0, 20), $ black ); } // Display the four numbers randomly on the canvas. The horizontal spacing and position of the characters are randomly generated according to a certain fluctuation range. $ Strx = rand (3, 8 ); For ($ I = 0; $ I <4; $ I ++ ){ $ Strpos = rand (1, 6 ); Imagestring ($ im, 5, $ strx, $ strpos, substr ($ num, $ I, 1), $ black ); $ Strx + = rand (8, 12 ); } ImagePNG ($ im ); ImageDestroy ($ im ); ?> |