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);
            $y = mt_rand(12,$this->height-24);
           // var_dump($x,$y);
            //echo "<br>";
            imagechar($this->image,8,$x,$y,$this->code[$i],$this->darkColor());
        }
        //exit;
    }

    //添加干扰颜色
    protected function drawDisturb(){
        for($i=0; $i < 150; $i++){
            $x = mt_rand(0,$this->width);
            $y = mt_rand(0,$this->height);
            imagesetpixel($this->image,$x,$y,$this->lightColor());
        }
    }

    //输出image
    protected function show(){
        header('Content-Type:image/png');
        imagepng($this->image);
    }
}

使用测试

/index.php

<?php
include 'Code.php';

$code = new Code();
$code->outImage();
//echo $code->code; //验证码字符串

浏览器访问 index.php

上一篇:(小白学java)Java 修饰符


下一篇:设计模式-模板方法代码示范