<? PHP $ code = new new Code (); $ code -> outImage (); class Code { // verify the number of code protected $ Number ; // this code type protected $ CodeType ; // image width protected $ width ; / / image height protected $ height ; // image resource protected $ image ; // this code string protected $ code ; / * * * function Undocumented * * @param integer $number * @param integer $codeType * @param integer $width * @param integer $height */ public function __construct($number = 4, $codeType = 2, $width = 100, $height = 50) { //初始化自己的成员属性 $this->number = $number; $this->codeType = $codeType; $this->width = $width; $this->height = $height; //Function codes generated $ the this -> code = $ the this -> the createCode (); } / * * * destructor * image resource release * / public function __destruct () { // release image resource imagedestroy ( $ the this -> Image ); } / * * * // protected by magic method of acquiring the object code * $ code = new new code (); * $ echo 3rd field - code> code; * * @param [type] $ name * @return void * / public function the __get ( $ name ) { IF (name $ == 'code' ) { return $ the this -> code; } return to false ; } / * * * get Code * * @return void * / protected function the createCode () { // through your verification code type to you generate different codes Switch ( $ the this -> CodeType) { Case 0: // pure digital $ code = $ the this -> getNumberCode (); BREAK ; Case . 1: // pure letters $ code = $ the this -> getCharCode (); BREAK ; Case 2: // letters, and numbers $ code = $ the this -> getNumCharCode (); BREAK ; default : Die ( 'these codes do not type ' ); } return $ code ; } / * * * yield pure digital codes * * @return void * / protected function getNumberCode () { // $startNum = pow(10, $this->number - 1); // $endNum = pow(10, $this->number) - 1; // $str = rand($startNum, $endNum); // return $str; $str = join('', range(0, 9)); return substr(str_shuffle($str), 0, $this->number); } /** * 生成纯字母验证码 * * @return void */ protected function getCharCode() { $str = join('', range( 'A', 'Z' )); $ STR = $ STR . the strtoupper ( $ STR ); return substr ( str_shuffle ( $ STR ), 0, $ the this -> Number ); } / * * * generate alphanumeric codes * * @return void * / protected function getNumCharCode () { $ numstr = the Join ( '', Range (0,. 9 )); $ STR = the Join ( '', Range('a', 'z')); $str = $numStr . $str . strtoupper($str); return substr(str_shuffle($str), 0, $this->number); } /** * 创建画布 * * @return void */ protected function createImage() { $this->image = imagecreatetruecolor($this->width, $this->height); } /** * Fill the background color * * @return void * / protected function fillBack () { ImageFill ( $ the this -> Image, 0, 0, $ the this -> lightColor ()); } / * * * randomly generated light color * * @ void return * / protected function lightColor () { return imagecolorallocate ( $ the this -> Image, the mt_rand (130., 255), the mt_rand (130., 255), the mt_rand (130., 255 )); } / * * * a dark color randomly generated * * @Return void * / protected function darkColor () { return imagecolorallocate ( $ the this -> Image, the mt_rand (0, 120), the mt_rand (0, 120), the mt_rand (0, 120 )); } / * * * The Videos string codes into the canvas * * @return void * / protected function 's drawChar () { $ width = ceil ( $ the this -> width / $ the this -> Number ); for ( $ I = 0; $i < $this->number; $i++) { $x = mt_rand($i * $width+5, ($i + 1) * $width - 10); $y = mt_rand(0, $this->height - 15); imagechar($this->image, 5, $x, $y, $this->code[$i], $this->darkColor()); } } /** * 添加干扰项 * * @return void */ protected function drawDisturb() { for ($i = 0; $i < 150; $i++) { $x = mt_rand(0, $this->width); $y = mt_rand(0, $this->height); imagesetpixel($this->image, $x, $y, $this->lightColor()); } } / ** * And outputs display * * @return void * / protected function Show () { header ( 'the Content-the Type: Image / PNG' ); imagepng ( $ the this -> Image); } public function outImage () { // create a canvas the this $ -> the createImage (); // fill the background color $ the this -> fillBack (); // the character string codes into the canvas painting $ the this -> 's drawChar (); // add distracter $ the this ->drawDisturb (); //And displaying output $ the this -> Show (); } }
running result: