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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...