php 封装验证码类

封装验证码类

/Code.php

<?php

/**
 * 验证码类
 * 使用方法
 * $code = new Code();
 * $code->outImage();//输出验证码图片
 * $code->code; // 获取验证码字符串
 */
class Code{
    //验证码个数
    protected $number;
    //验证码类型
    protected $codeType;
    //图像宽度
    protected $width;
    //图像高度
    protected $height;
    //图像资源
    protected $image;
    //验证码字符串
    protected $code;

    //构造方法
    public function __construct($number = 4,$codeType = 1,$width = 100,$height = 50)
    {
        //初始化参数
        $this->number = $number;
        $this->codeType = $codeType;
        $this->width = $width;
        $this->height = $height;

        //生成验证码
        $this->code = $this->createCode();
    }

    //生成验证码
    protected function createCode(){
        switch ($this->codeType){
            case 1://纯数字
                $code = $this->getNumberCode();
                break;
            case 2://纯字母
                $code = $this->getCharCode();
                break;
            case 3://数字和字母混合
                $code = $this->getNumCharCode();
                break;
            default:
                throw new Exception('不支持的验证码类型');
        }
        return $code;
    }

    //生成纯数字验证码
    protected function getNumberCode(){
        $str = join('',range(0,9));
        return substr(str_shuffle($str),0,$this->number);
    }

    //生成纯字母验证码
    protected function getCharCode(){
        $str = join('',range('a','z'));
        $str = $str . strtoupper($str);
        return substr(str_shuffle($str),0,$this->number);

    }

    //生成数字和字母混合验证码
    protected function getNumCharCode(){
        $numStr = join('',range(0,9));
        $str = join('',range('a','z'));
        $str = $numStr . $str . strtoupper($str);
        return substr(str_shuffle($str),0,$this->number);
    }

    //魔术方法
    public function __get($name){
        if($name==$this->code){
            return $this->code;
        }
        return false;
    }

    //销毁
    public function __destruct()
    {
        imagedestroy($this->image);
    }

    //输出image
    public function outImage(){
        //创建画布
        $this->createImage();
        //填充背景色
        $this->fillBack();
        //将验证码画到画布中
        $this->drawChar();
        //添加干扰元素
        $this->drawDisturb();
        //输出并显示
        $this->show();
    }

    //创建画布
    protected function createImage(){
        $this->image = imagecreatetruecolor($this->width,$this->height);
    }

    //填充背景颜色
    protected function fillBack(){
        imagefill($this->image,0,0,$this->lightColor());
    }

    //生成浅颜色
    protected function lightColor(){
        return imagecolorallocate($this->image,mt_rand(180,255),mt_rand(180,255),mt_rand(180,255));
    }

    //生成深颜色
    protected function darkColor(){
        return imagecolorallocate($this->image,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
    }

    //将验证码画到画布中
    protected function drawChar(){
        $width = ceil($this->width / $this->number);
        //var_dump($width);
        //echo "<br>";
        for($i=0; $i < $this->number; $i++){
            $x = mt_rand($i*$width+5,($i + 1)*$width-12);