Write the captcha class

1. configuration file config.php

1 ? < PHP
 2  
. 3      / * *
 . 4       * Code Type codeType int 0: digital-1: a plain string 2: mixed numeric and string
 5       * length codes int length
 . 6       * image width int width
 . 7       * Image Height height int
 . 8       * / 
. 9       return 
10       [
 . 11        'CodeType' => 2,
 12 is        'length' =>. 5,
 13 is        'width' => 400,
 14        'height' => 200 is,
 15       ];
config.php

2. Generate class codes

? < PHP
     // definition of class codes 
    class Code {
         Private  $ length ;     // this code length 
        Private  $ CodeType ;   // Type 
        Private  $ code ;       // codes 
        Private  $ width ;      // width 
        Private  $ height ;     // height 
        Private  $ img ;        // pictures resources 

        public  function __construct () 
        { 
            // the introduction of profiles 
            $ the this -> config = require_once './config.php';
            $this->length = $this->config['length'];
            $this->codeType = $this->config['codeType'];
            $this->width = $this->config['width'];
            $this->height = $this->config['height'];
            $this->createCode();
        }

        protected function createCode()
        {
            switch($this->codeType){
                case0:
                     $ the this -> code = $ the this -> getNumberCode ();
                     BREAK ;
                 Case . 1:
                     $ the this -> code = $ the this -> getCharCode ();
                     BREAK ;
                 Case 2:
                     $ the this -> code = $ the this -> getNumCharCode ();
                     BREAK ;
                 default :
                     Die ( 'type error codes, please reenter!' );
                     BREAK ;  
            }
        }

        public function getCode()
        {
            return $this->code;
        }

        private function getNumberCode()
        {
            $number = join('',range(0,9));
            return $this->setCode($number);
        }

        private function getCharCode()
        {
            $str = join('',range('a','z'));
            $str .= strtoUpper($str);
            return $this->setCode($str);
        }

        private function getNumCharCode()
        {
            $number = join('',range(0,9));
            $str = join('',range('a','z'));
            $code = strtoUpper($str).$str.$number;
            return $this->setCode($code);
        }

        private functionsetCode ( $ String ) 
        { 
            return  substr ( str_shuffle ( $ String ), 0, $ the this -> length); 
        } 

        // output image 
        public  function GETIMG () 
        { 
            // new canvas 
            $ the this -> createImg ();
             // canvas background fill color 
            $ the this -> fillBackground ();
             // the codes written canvas 
            $ the this -> fillCode ();
             // Add nuisances 
            $ the this -> setDistubPoint ();
             //设置干扰线
            $this->setDisEarc();
            //显示图像
            $this->showImg();
        }

        protected function createImg()
        {
            $this->img = imagecreatetruecolor($this->width,$this->height);
        }

        protected function fillBackground()
        {
            imagefill($this->img,0,0,$this->lightColor());
        }

        private function lightColor()
        {
            return imagecolorallocate($this->img,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));
        }

        private function darkColor()
        {
            return imagecolorallocate($this->img,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
        }

        protected function fillCode()
        {
            $width = ceil($this->width/$this->length);
            $height = $this->height/2;
            for($i=0;$i<$this->length;$i++){
                $x = mt_rand($i*$width+10,($i+1)*$width-10);
                $y = mt_rand($height-10,$height+10);
                imagechar($this->img,5,$x,$y,$this->code[$i],$this->darkColor());
            }
        }

        //设置干扰点
        protected function setDistubPoint()
        {
            for($i=0;$i<1000;$i++){
                $x = mt_rand(0,$this->width);
                $y = mt_rand(0,$this->height);
                imagesetpixel($this->img,$x,$y,$this->darkColor());
            }
        }

        //设置干扰线段
        protected function setDisEarc()
        {
            for($i=0;$i<2;$i++){
                imagearc ( $this->img ,  rand(0,$this->width) ,  rand(0,$this->height) ,  rand($this->width,$this->width*2) ,  rand($this->height,$this->height*2) ,  rand(0,100) ,  rand(280,270) ,  $this->darkColor() );
            }
        }

        protected function showImg()
        {
            header('Content-type:image/png');
            imagepng($this->img);
        }
    }


    $code = new Code();

    $code ->getImg();
ValidateCode.php

Guess you like

Origin www.cnblogs.com/jianbing123/p/12080518.html