Php-encapsulated Verification Code Tool
This example describes the php-encapsulated Verification Code tool class. We will share this with you for your reference. The details are as follows:
<? Php // Verification Code tool class Captcha {// attribute private $ width; private $ height; private $ fontsize; private $ pixes; private $ lines; private $ str_len; /** constructor * @ param1 array $ arr = array (), the associated array of the initialization attribute */public function _ construct ($ arr = array ()) {// initialize $ this-> width = isset ($ arr ['width'])? $ Arr ['width']: $ GLOBALS ['config'] ['captcha '] ['width']; $ this-> height = isset ($ arr ['height'])? $ Arr ['height']: $ GLOBALS ['config'] ['captcha '] ['height']; $ this-> fontsize = isset ($ arr ['fontsize'])? $ Arr ['fontsize']: $ GLOBALS ['config'] ['captcha '] ['fontsize']; $ this-> pixes = isset ($ arr ['pixes'])? $ Arr ['pixes']: $ GLOBALS ['config'] ['captcha '] ['pixes']; $ this-> lines = isset ($ arr ['lines'])? $ Arr ['lines']: $ GLOBALS ['config'] ['captcha '] ['lines']; $ this-> str_len = isset ($ arr ['str _ len'])? $ Arr ['str _ len']: $ GLOBALS ['config'] ['captcha '] ['str _ len'];} /** generate Verification Code image */public function generate () {// create a canvas $ img = imagecreatetruecolor ($ this-> width, $ this-> height ); // specify the background color $ bg_color = imagecolorallocate ($ img, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255); imagefill ($ img, $ bg_color ); // create the interference line $ this-> getLines ($ img); // Add the interference point $ this-> getPixels ($ img ); // Add the verification code text $ captcha = $ this-> getCaptcha (); // The text color $ str_color = imagecolorallocate ($ img, mt_rand (0,100), mt_rand (0,100 ), mt_rand (0,100); // write text // calculate the starting position of the text to appear $ start_x = ceil ($ this-> width/2)-25; $ start_y = ceil ($ this-> height/2)-8; if (imagestring ($ img, $ this-> fontsize, $ start_x, $ start_y, $ captcha, $ str_color) {// success: output verification code header ('content-type: image/png '); imagepng ($ img );} else {// failed return false;}/** random string for obtaining the Verification Code * @ return string $ captcha, Random verification code text */private function getCaptcha () {// obtain the random string $ str = implode ('', array_merge (range ('A', 'z'), range ('A', 'z '), range (); // random $ captcha = ''; // Save the random string for ($ I = 0, $ len = strlen ($ str ); $ I <$ this-> str_len; $ I ++) {// a random character $ captcha. = $ str [mt_rand (0, $ len-1)]. '';} // save data to session $ _ SESSION ['captcha '] = str_replace ('', '', $ captcha); // return value return $ captcha ;} /** add interference points * @ param1 resource $ img */private function getPixels ($ img) {// Add interference points for ($ I = 0; $ I <$ this-> pixes; $ I ++) {// assign color $ pixel_color = imagecolorallocate ($ img, mt_rand (100,150), mt_rand (100,150 ), mt_rand (100,150); // image point imagesetpixel ($ img, mt_rand (0, $ this-> width), mt_rand (0, $ this-> height ), $ pixel_color) ;}}/** add interference line * @ param1 resource $ img, image resource for adding interference lines */private function getLines ($ img) {// Add interference lines for ($ I = 0; $ I <$ this-> lines; $ I ++) {// assign color $ line_color = imagecolorallocate ($ img, mt_rand (150,200), mt_rand (150,200), mt_rand (150,200); // draw line imageline ($ img, mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), $ line_color );}} /** Verification Code * @ param1 string $ captcha, user-submitted verification code * @ return bool, true is returned for success, false is returned for failure */public static function checkCaptcha ($ captcha) {// The verification code is case-insensitive. return (strtolower ($ captcha) === strtolower ($ _ SESSION ['captcha ']);}