This article will share with you how to use the GD library of php to implement the verification code function and all the code. if you need it, you can refer
<? Php require_once 'string. func. php '; // use the GD library for verification code/*** add verification text * @ param int $ type * @ param int $ length */function buildRandomString ($ type = 1, $ length = 4) {$ row = ''; if ($ type = 1) {$ row = join ('', range (0, 9 ));} else if ($ type = 2) {$ row = join ('', array_merge (range ('A', 'z'), range ('A ', 'Z');} else if ($ type = 3) {$ row = join ('', array_merge (range ('A', 'z '), range ('A', 'z'), range (0, 9) ;}; $ row = str_shuffle ($ row); $ row = substr ($ row, 0, $ length); return $ row ;} /*** generate a thumbnail ** @ param int $ type // contains numbers or English characters * @ param int $ length * @ param int $ pixel interferes with the density of small dots * @ param int $ dst_h interference line density * @ param string // the name of the verification code in $ _ SESSION */function verifyImage ($ type = 1, $ length = 4, $ pixel = 0, $ line = 0, $ sess_name = "verify") {// session_start (); // create canvas $ width = 100; $ height = 40; $ image = imagecreatetruecolor ($ width, $ height); $ white = imagecolorallocate ($ image, 255,255,255); $ black = imagecolorallocate ($ image, 0, 0, 0); // fill the canvas imagefilledrectangle ($ image, 1, 1, $ width-2, $ height-2, $ white) with a filled rectangle ); $ chars = buildRandomString ($ type, $ length); $ _ SESSION [$ sess_name] = $ chars; // $ fontfiles = array ("MSYH. TTF "," MSYHBD. TTF "," SIMLI. TTF "," SIMSUN. TTC "," SIMYOU. TTF "," STZHONGS. TTF "); $ fontfiles = array (" SIMKAI. TTF "); // because the font file is relatively large, only one font will be retained. if you need it, you can add the font by yourself. the fonts are in the fonts folder on your computer, run the input fonts command to view the corresponding font for ($ I = 0; $ I <$ length; $ I ++) {$ size = mt_rand (14, 18 ); $ angle = mt_rand (-15, 15); $ x = 5 + $ I * $ size; $ y = mt_rand (20, 26); $ fontfile = ".. /fonts /". $ fontfiles [mt_rand (0, count ($ fontfiles)-1)]; $ color = imagecolorallocate ($ image, mt_rand (50, 90), mt_rand (80,200 ), mt_rand (90,180); $ text = substr ($ chars, $ I, 1); imagettftext ($ image, $ size, $ angle, $ x, $ y, $ color, $ fontfile, $ text);} if ($ pixel) {for ($ I = 0; $ I <50; $ I ++) {imagesetpixel ($ image, mt_rand (0, $ width-1), mt_rand (0, $ height-1), $ black) ;}} if ($ line) {for ($ I = 1; $ I <$ line; $ I ++) {$ color = imagecolorallocate ($ image, mt_rand (50, 90), mt_rand (80,200), mt_rand (90,180 )); imageline ($ image, mt_rand (0, $ width-1), mt_rand (0, $ height-1), mt_rand (0, $ width-1), mt_rand (0, $ height-1), $ color) ;}} header ("content-type: image/gif"); imagegif ($ image); imagedestroy ($ image );}
Key points:
1. if session_start () is not stated before, it must be stated;
2. you can enter fonts in cmf to download the fonts folder you have defined;
3. for $ _ SESSION [$ sess_name], you can use $ _ POST to obtain the verification code entered by the user for comparison.