验证码图片类的编写

1.配置文件config.php

 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.生成验证码类

<?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

猜你喜欢

转载自www.cnblogs.com/jianbing123/p/12080518.html