Copy codeThe Code is as follows:
<? Php
Public function captcha (){
$ Font_dir = $ _ SERVER ["DOCUMENT_ROOT"]. "your_ttf_file.ttf"; // Library
$ Img_w = 58; // set the Image Width
$ Img_h = 20; // set the Image Height.
$ Font_size = 11; // font size
$ Angle_l =-10; // left angle
$ Angle_r = 10; // right angle
$ Code_str = "ABCDEFGHJKLMNPQRSTUVWXYZ36 ";
$ Word_len = 4; // number of digits of the Verification Code
$ Padding = 5; // interval between two texts
$ Margin = 2; // left margin
$ Base_line = 15; // text baseline position
$ Base_line_offset = 2; // baseline offset
$ Pixel_num = 3; // The base number of vertices.
$ Pixel_color = 8; // only the total number of points in the $ pixel_color color is $ pixel_num * $ pixel_color
$ Noise_font_size = 1; // miscellaneous font size
$ Session_key = "my. xoyo_captcha"; // custom session key name
Header ("Cache-Control: no-cache, must-revalidate ");
Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT ");
Header ("Pragma: no-cache ");
Header ("Cache-control: private ");
Header ('content-Type: image/png ');
Session_start ();
$ Word = "";
$ Code_str_len = strlen ($ code_str)-1;
For ($ I = 0; $ I <$ word_len; $ I ++ ){
$ Word. = $ code_str [rand (0, $ code_str_len)];
}
$ _ SESSION [$ session_key] = strtolower ($ word );
$ Image = imagecreatetruecolor ($ img_w, $ img_h );
Imagefilledrectangle ($ image, 0, 0, $ img_w-1, $ img_h-1, imagecolorallocate ($ image, mt_rand (235,255), mt_rand (235,255 ), mt_rand (235,255 )));
// Draw the Miscellaneous
For ($ I = 0; $ I <$ pixel_color; $ I ++ ){
$ Noise_color = imagecolorallocate ($ image, mt_rand (150,225), mt_rand (150,225), mt_rand (150,225 ));
For ($ j = 0; $ j <$ pixel_num; $ j ++ ){
Imagestring ($ image, $ noise_font_size, mt_rand (-10, $ img_w), mt_rand (-10, $ img_h), $ code_str [mt_rand (0, $ code_str_len)], $ noise_color );
}
}
// Draw text
For ($ I = 0; $ I <$ word_len; ++ $ I ){
$ Color = imagecolorallocate ($ image, mt_rand (0,100), mt_rand (20,120), mt_rand (50,150 ));
Imagettftext ($ image, $ font_size, mt_rand ($ angle_l, $ angle_r), $ margin, mt_rand ($ base_line-$ base_line_offset, $ base_line + $ base_line_offset), $ color, $ font, mb_substr ($ word, $ I, 1, 'utf-8 '));
$ Margin + = (imagefontwidth ($ font_size) + $ padding );
}
Imagepng ($ image );
Imagedestroy ($ image );
Exit;
}