laravel 验证码 类

这个类库用于生成安全的验证码,支持宽度、高度、字符数、字符类型的配置,并包含干扰元素如雪花点和线条,能以PNG格式保存或直接输出到浏览器。通过设置不同的参数,可以创建不同特性的验证码,确保了网页验证的安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

验证码:


class SecurityCodeRepository
{
    private $width;     //验证码的宽
    private $height;    //验证码的高
    private $codeNum;   //验证码中的字符数
    private $type;      //验证码中字符的类型  1.数字  2.字母  3.字母加数字(默认)
    private $session;   //存储验证码
    private $fontStyle; //验证码字体样式,需引入字体文件
    private $dot;       //验证码雪花点数
    private $line;      //验证码干扰线条
    private $image;     //验证码图片
    private $chars;     //验证码字符串
    private $bgcolor;   //背景颜色
    private $isUrl;     //是否返回路径
    private $saveDir;   //保存目录

    function __construct($config =array()){
        //默认参数
        $width = 100;
        $height = 40;
        $codeNum = 4;
        $type = 3;
        $session = 'securityCode';
        $fontStyle = public_path().'/fz.TTF';
        $dot = 50;
        $line = 4;
        $bgcolor = array(
            'red' => 255,
            'blue' => 255,
            'green' => 255
        );
        $isUrl = false;
        $saveDir = '.';
        foreach ($config as $key => $value){
            switch ($key){
                case 'width':
                    $width = $value ? $value : $width;
                    break;
                case 'height':
                    $height = $value ? $value : $height;
                    break;
                case 'codeNum':
                    $codeNum = $value ? $value : $codeNum;
                    break;
                case 'type':
                    $type = $value ? $value : $type;
                    break;
                case 'session':
                    $session = $value ? $value : $session;
                    break;
                case 'fontStyle':
                    $fontStyle = $value ? $value : $fontStyle;
                    break;
                case 'dot':
                    $dot = $value ? $value : $dot;
                    break;
                case 'line':
                    $line = $value ? $value : $line;
                    break;
                case 'bgcolor':
                    $bgcolor = $value ? $value : $bgcolor;
                    break;
                case 'isUrl':
                    $isUrl = $value ? $value : $isUrl;
                    break;
                case 'saveDir':
                    $saveDir = $value ? $value : $saveDir;
                    break;
            }
        }

        $this->width = $width;
        $this->height = $height;
        $this->codeNum = $codeNum;
        $this->type = $type;
        $this->session = $session;
        $this->fontStyle = $fontStyle;
        $this->dot = $dot;
        $this->line = $line;
        $this->bgcolor = $bgcolor;
        $this->isUrl = $isUrl;
        $this->saveDir = $saveDir;
        $this->image = $this->createImage();
    }

    //生成图片
    private function createImage(){
        $image = imagecreatetruecolor($this->width,$this->height);
        return $image;
    }

    //生成验证码字符
    private function createChar($chr){
        if ($this->type == 1){
            $chars = implode('',range(0,9));   //生成数字
        }else if($this->type == 2){
            $chars = implode('',array_merge(range('A','Z'),range('a','z')));  //生成字母
        }else if ($this->type == 3){
            $chars = implode('',array_merge(range('A','Z'),range('a','z'),range(0,9))); //生成字母数字
        }
        $chars = str_shuffle($chars);  //打乱字符串的排序
        if ($this->codeNum > strlen($chars)){
            exit('数字过大');  //判断截取的个数是不是超过字符串的长度
        }

        $chars = substr($chars,0,$this->codeNum);  //截取字符
        return $chars;
    }

    //生成干扰元素
    private function interferon(){
        for ($i = 0; $i <$this->line;$i++){
            $color = imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            imageline($this->image,mt_rand(0,$this->width -1),mt_rand(0,$this->height -1),mt_rand(0,$this->width -1),mt_rand(0,$this->height-1),$color);
        }

        for ($i = 0;$i < $this->dot;$i++){
            $color = imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            imagesetpixel($this->image,mt_rand(0, $this->width - 1), mt_rand(0, $this->height - 1), $color);
        }
    }

    //展现图片
    public function getImage($char){
        $color = imagecolorallocate($this->image,$this->bgcolor['red'],$this->bgcolor['green'],$this->bgcolor['blue']);
        imagefilledrectangle($this->image,0,0,$this->width,$this->height,$color);
        for ($i = 0;$i <$this->codeNum;$i++){
            $size = floor($this->height / 2);
            $angle = mt_rand(-15,15);
            $x = 10 + $i * $size;
            $y = $size + floor($size / 2);
            $color = imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            $text = substr($char,$i,1);
            imagettftext($this->image,$size,$angle,$x,$y,$color,$this->fontStyle,$text);
        }

        $this->interferon();
        if ($this->isUrl){
            $fileName = $this->saveDir.time().'.png';
            imagepng($this->image,$fileName);
            imagedestroy($this->image);
            return $fileName;
        }else{
            header("content-type:image/png");
            imagepng($this->image);
            imagedestroy($this->image);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值