<? Php // 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 (); $ y2 = rand (); $ y3 = rand (); $ y4 = rand ); 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 ); ?> |