驗證碼圖片類的編寫

1.配置文件config.phpphp

 1 <?php
 2 
 3     /**
 4      * 驗證碼類型 codeType int 0:純數字 1:純字符串 2:數字和字符串混合
 5      * 驗證碼長度 length   int
 6      * 圖片寬度  width int
 7      * 圖片高度 height int
 8      */
 9      return
10      [
11       'codeType' => 2,
12       'length' => 5,
13       'width' => 400,
14       'height' => 200,
15      ];
config.php

2.生成驗證碼類ide

<?php
    //定義驗證碼類
    class Code{
        private $length;    //驗證碼長度
        private $codeType;  //類型
        private $code;      //驗證碼
        private $width;     //寬度
        private $height;    //高度
        private $img;       //圖片資源

        public function __construct()
        {
            //引入配置文件
            $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){
                case 0:
                    $this->code = $this->getNumberCode();
                    break;
                case 1:
                    $this->code = $this->getCharCode();
                    break;
                case 2:
                    $this->code = $this->getNumCharCode();
                    break;
                default:
                    die('驗證碼類型錯誤,請從新輸入!');
                    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 function setCode($string)
        {
            return substr(str_shuffle($string),0,$this->length);
        }

        //輸出圖像
        public function getImg()
        {
            //新建畫布
            $this->createImg();
            //畫布填充背景色
            $this->fillBackground();
            //將驗證碼寫入畫布
            $this->fillCode();
            //加入干擾點
            $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
相關文章
相關標籤/搜索