Ecshop verification code, ecshop Verification Code call
<? Php // copy the ecshop Verification Code (four uppercase letters, numbers, and backgrounds) // process the code value (consisting of four uppercase letters and numbers) // all possible character sets $ chars = 'abcdefghijklmnopqrstuvwxyz0123456789 '; $ chars_len = strlen ($ chars); // set length // randomly select $ code_len = 4; // verification code length $ code = ''; // verification code value initialization for ($ I = 0; $ I <$ code_len; ++ $ I) {// randomly obtain a character subscript $ rand_index = mt_rand (0, $ chars_len-1); // use the subscript operation of the string to obtain the selected character $ code. = $ chars [$ rand_index];} // echo $ code; // stored in session (for verification) session_start (); $ _ SESSION ['code'] = $ code; // Verification code Image (known background image) // processing background $ bg_file = '. /captcha/captcha_bg '. mt_rand (1, 5 ). '.jpg '; // create a canvas $ image = imagecreatefromjpeg ($ bg_file) based on the image; // a simple function (imageString ();) that writes a string on the canvas ();) // imageString (canvas, Font, position X, position y, string content, color); // Font: imagestring function, built-in font used. It is represented by 1-5. The position is determined by the coordinates in the upper left corner of the string. Color also needs to be pre-allocated. Imagecolorallocate (); // assign the font color (black or white at random) if (mt_rand () = 1) {$ str_color = imagecolorallocate ($ image, 0 ); // black} else {$ str_color = imagecolorallocate ($ image, 255, 0xff, 255); // white} // Built-in 5 font $ font = 5; // position // canvas size $ image_w = imagesx ($ image); $ image_h = imagesy ($ image ); // get the font width and height $ font_w = imagefontwidth ($ font); $ font_h = imagefontheight ($ font ); // obtain the string width and height $ str_w = $ font_w * $ code_len; $ str_h = $ font_h; // calculates the position $ str_x = ($ image_w-$ str_w)/2; $ str_y = ($ image_h-$ str_h)/2; // string imagestring ($ image, $ font, $ str_x, $ str_y, $ code, $ str_color ); // output and destroy the canvas header ("content-type: image/jpeg"); imagejpeg ($ image); imagedestroy ($ image );
Tool for encapsulating verification codes:
<? Php // Verification Code tool class (encapsulate all the verification code operations in this class) class Captcha {/* generate the Verification Code */public function makeImage ($ code_len = 4) {// copy the ecshop Verification Code (four uppercase letters, numbers, and backgrounds) // process the code value (consisting of four uppercase letters and digits) // all possible character sets $ chars = 'abcdefghijklmnopqrstuvwxyz0123456789 '; $ chars_len = strlen ($ chars); // set length // randomly select $ code = ''; // initialize the verification code value for ($ I = 0; $ I <$ code_len; ++ $ I) {// obtain a random character subscript $ rand_index = mt_rand (0, $ chars_len-1); // use the subscript operation of the string to obtain the selected character $ code. = $ chars [$ rand_index ];} // Echo $ code; // stored in the session (for verification) @ session_start (); $ _ SESSION ['code'] = $ code; // Verification Code image (known background image) // processing background $ bg_file = TOOL. '. /captcha/captcha_bg '. mt_rand (1, 5 ). '.jpg '; // create a canvas $ image = imagecreatefromjpeg ($ bg_file) based on the image; // a simple function (imageString ();) that writes a string on the canvas ();) // imageString (canvas, Font, position X, position y, string content, color); // Font: imagestring function, built-in font used. It is represented by 1-5. The position is determined by the coordinates in the upper left corner of the string. Color also needs to be pre-allocated. Imagecolorallocate (); // assign the font color (black or white at random) if (mt_rand () = 1) {$ str_color = imagecolorallocate ($ image, 0 ); // black} else {$ str_color = imagecolorallocate ($ image, 255, 0xff, 255); // white} // Built-in 5 font $ font = 5; // position // canvas size $ image_w = imagesx ($ image); $ image_h = imagesy ($ image ); // get the font width and height $ font_w = imagefontwidth ($ font); $ font_h = imagefontheight ($ font ); // obtain the string width and height $ str_w = $ font_w * $ code_len; $ str_h = $ font_h; // calculates the position $ str_x = ($ image_w-$ str_w)/2; $ str_y = ($ image_h-$ str_h)/2; // string imagestring ($ image, $ font, $ str_x, $ str_y, $ code, $ str_color ); // output and destroy the canvas header ("content-type: image/jpeg"); imagejpeg ($ image); imagedestroy ($ image );}}