$ Ch_str = "You want to generate Chinese character verification code "; $ Str = array (); For ($ I = 0; $ I <strlen ($ ch_str); $ I + = 3) { $ Str [] = $ ch_str [$ I]. $ ch_str [$ I + 1]. $ ch_str [$ I + 2]; } // The length and height of the image $ Image_x = 200; $ Image_y = 100; $ Im = imagecreate ($ image_x, $ image_y ); // The background color of the image is white. $ Bkg = imagecolorallocate ($ im, 255,255,255 ); // Display the font style. Put the file in the corresponding directory. If you do not have any file, find one in the font file of window. $ Fnt = "simfang. ttf "; // Assign some color to the image $ White = imagecolorallocate ($ im, 234,185, 95 ); // Draw an elliptical arc on the image and specify the coordinate point Imagearc ($ im, 150, 8, 20, 20, 75,170, $ white ); Imagearc ($ im, 180, 75,175, 30, $ white ); // Draw a line segment on the image and specify the coordinate point Imageline ($ im, 20, 20, 180,30, $ white ); Imageline ($ im, 20, 18, 170,50, $ white ); Imageline ($ im, 25, 50, 80, 50, $ white ); // Number of random points $ Noise_num = 3000; $ Line_num = 80; // Color of various chaotic characters $ Rectangle_color = imagecolorallocate ($ im, 0xaa, 0xaa, 0xaa ); $ Noise_color = imagecolorallocate ($ im, 0x00,0x00,0x00 ); $ Font_color = imagecolorallocate ($ im, 0x00,0x00,0x00 ); For ($ I = 0; $ I <$ noise_num; $ I ++) { // Draw a single pixel on a coordinate point, which is defined above and is black. Imagesetpixel ($ im, mt_rand (0, $ image_x), mt_rand (0, $ image_y), $ noise_color ); } For ($ I = 0; $ I <$ line_num; $ I ++) { $ Line_color = imagecolorallocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 )); // Draw a line between two coordinate points, and the color is defined above Imageline ($ im, mt_rand (0, $ image_x), mt_rand (0, $ image_y), mt_rand (0, $ image_x), mt_rand (0, $ image_y ), $ line_color ); } $ Randnum = rand (0, count ($ str)-4 ); // The value is an even number. If ($ randnum % 2) { $ Randnum + = 1; } $ Str1 = $ str [$ randnum]. $ str [$ randnum + 1]; For ($ I = 0; $ I <2; $ I ++) { Imagettftext ($ im, rand (28, 32), rand (0, 70), rand ($ image_x/4) * $ I + $ image_x/10, ($ image_x/4) * $ I + $ image_x/8), rand ($ image_y/2 + $ image_y/10, $ image_y/2 + $ image_y/5), $ font_color, $ fnt, $ str [$ randnum + $ I]); } Imagepng ($ im ); Imagedestroy ($ im ); // Generate Chinese verification code 2 $ Str = "Chinese characters "; $ Image_x = 110; $ Image_y = 110; $ Im = imagecreate ($ image_x, $ image_y ); $ Bkg = imagecolorallocate ($ im, 255,255,255 ); $ Fnt = "hb. ttf"; // the font style displayed. $ White = imagecolorallocate ($ im, 234,185, 95 ); Imagearc ($ im, 150, 8, 20, 20, 75,170, $ white ); Imagearc ($ im, 180, 75,175, 30, $ white ); Imageline ($ im, 20, 20, 180,30, $ white ); Imageline ($ im, 20, 18, 170,50, $ white ); Imageline ($ im, 25, 50, 80, 50, $ white ); $ Noise_num = 3000; $ Line_num = 80; Imagecolorallocate ($ im, 0xff, 0xff, 0xff ); $ Rectangle_color = imagecolorallocate ($ im, 0xaa, 0xaa, 0xaa ); $ Noise_color = imagecolorallocate ($ im, 0x00,0x00,0x00 ); $ Font_color = imagecolorallocate ($ im, 0x00,0x00,0x00 ); $ Line_color = imagecolorallocate ($ im, 0x00,0x00,0x00 ); For ($ I = 0; $ I <$ noise_num; $ I ++) Imagesetpixel ($ im, mt_rand (0, $ image_x), mt_rand (0, $ image_y), $ noise_color ); For ($ I = 0; $ I <$ line_num; $ I ++) Imageline ($ im, mt_rand (0, $ image_x), mt_rand (0, $ image_y), mt_rand (0, $ image_x), mt_rand (0, $ image_y ), $ line_color ); $ Randnum = rand (0, strlen ($ str)-4 ); If ($ randnum % 2) $ randnum + = 1; $ Str1 = substr ($ str, $ randnum, 4 ); $ Str2 = iconv ("gb2312", "UTF-8", $ str1); // verify that Chinese characters are in $ str1 Imagettftext ($ im, rand (28, 32), rand (70,100), rand (25, 27), rand (), $ font_color, $ fnt, $ str2 ); Imagepng ($ im ); Imagedestroy ($ im ); // Place Chinese characters in an array /* The gd function only accepts UTF-8 encoded text. Therefore, encode and convert the text before writing it. Php's built-in iconv and mbstring libraries can do this. */ $ Randcode = array ('pet '); $ Codetable = array (); $ Fp = fopen ("gb2312.txt", "r "); While ($ line = fgets ($ fp )) $ Codetable [hexdec (substr ($ line,)] = substr ($ line ); Fclose ($ fp ); // Convert gb2312 to utf8 Function gb2utf8 ($ gbstr) { Global $ codetable; If (trim ($ gbstr) = "") Return $ gbstr; $ Ret = ""; $ Utf8 = ""; While ($ gbstr) { If (ord (substr ($ gbstr, 127)>) { $ Thisw = substr ($ gbstr, 0, 2 ); $ Gbstr = substr ($ gbstr, 2, strlen ($ gbstr )); $ Utf8 = ""; @ $ Utf8 = u2utf8 (hexdec ($ codetable [hexdec (bin2hex ($ thisw)-0x8080]); If ($ utf8! = "") For ($ I = 0; $ I <strlen ($ utf8); $ I + = 3) $ Ret. = chr (substr ($ utf8, $ I, 3 )); } Else { $ Ret. = substr ($ gbstr, 0, 1 ); $ Gbstr = substr ($ gbstr, 1, strlen ($ gbstr )); } } Return $ ret; } // Unicode to utf8 Function u2utf8 ($ c) { $ Str = ""; If ($ c <0x80) $ Str. = $ c; Elseif ($ c <0x800) { $ Str. = (0xc0 | $ c> 6 ); $ Str. = (0x80 | $ c & 0x3f ); } Elseif ($ c <0x10000) { $ Str. = (0xe0 | $ c> 12 ); $ Str. = (0x80 | $ c> 6 & 0x3f ); $ Str. = (0x80 | $ c & 0x3f ); } Elseif ($ c <0x200000) { $ Str. = (0xf0 | $ c> 18 ); $ Str. = (0x80 | $ c> 12 & 0x3f ); $ Str. = (0x80 | $ c> 6 & 0x3f ); $ Str. = (0x80 | $ c & 0x3f ); } Return $ str; } // Generate an additional code Function create_excode ($ length) { Global $ randcode; Header ("content-type: image/png "); $ Image_x = $ length * 30; // image width $ Image_y = 40; // Image height $ Noise_num = 80 * $ length; // number of vertices $ Line_num = $ length-2; // Number of interference lines $ Image = imagecreate ($ image_x, $ image_y ); Imagecolorallocate ($ image, 0xff, 0xff, 0xff); // you can specify the background color. $ Rectangle_color = imagecolorallocate ($ image, 0xaa, 0xaa, 0xaa); // border color $ Noise_color = imagecolorallocate ($ image, 0 x, 0 x); // Miscellaneous color $ Font_color = imagecolorallocate ($ image, 0x00,0x00,0x00); // font color $ Line_color = imagecolorallocate ($ image, 0x33,0x33,0x33); // interference line color // Add the Miscellaneous For ($ I = 0; $ I <$ noise_num; $ I ++) Imagesetpixel ($ image, mt_rand (0, $ image_x), mt_rand (0, $ image_y), $ noise_color ); $ Font_face = "simkai. ttf"; // font $ X = 2; $ Session_code = ''; For ($ I = 0; $ I <$ length; $ I ++) { $ Code = $ randcode [mt_rand (0, count ($ randcode)-1)]; Imagettftext ($ image, 18, mt_rand (-6, 6), $ x, 29, $ font_color, $ font_face, gb2utf8 ($ code )); $ X + = 30; $ Session_code. = $ code; } @ Session_start (); $ _ Session ['excode'] = $ session_code; // put the value of the additional code in the session // Add interference lines For ($ I = 0; $ I <$ line_num; $ I ++) Imageline ($ image, mt_rand (0, $ image_x), mt_rand (0, $ image_y ), Mt_rand (0, $ image_x), mt_rand (0, $ image_y), $ line_color ); Imagerectangle ($ image, $ image_x-1, $ image_y-1, $ rectangle_color); // add a border Imagepng ($ image ); Imagedestroy ($ image ); } Create_excode (6 ); |