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      ];

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();