温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP验证码类

发布时间:2020-07-11 11:16:18 来源:网络 阅读:427 作者:woaijorden 栏目:web开发
class Captcha{
    //验证码宽
    private $width;
    //验证码高度
    private $height;
    //验证码长度
    private $len;
    //验证码范围
    private $str =  "abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
    //字体地址
    private $font_dir = 'F:\phpStudy\PHPTutorial\WWW\php\code\4.ttf';
    //验证码资源
    private $img;
    //验证码字符串
    private $code;

    public function __construct($config=['width'=>200,'height'=>50,'len'=>4]){
        $this->width=$config['width'];
        $this->height=$config['height'];
        $this->len=$config['len'];

    }

    public function entry(){
        $this->createBg();
        $this->getCode();
        $this->setCode();
        $this->setLine();
        $this->setDot();
        $this->setBg();

    }

    //生成画布
    private function createBg(){
        $this->img = imagecreate($this->width,$this->height);
        //分配验证码的背景颜色
        imagecolorallocate($this->img,255,255,255);

    }

    //生成随机字符串
    private function getCode(){
        for($i=0;$i<$this->len;$i++){
            $key = mt_rand(0,strlen($this->str)-1);
            $this->code .= $this->str[$key];
        }
        $this->setSession();
    }

    //把验证码存入session之后和用户输入的验证码做比较
    private function setSession(){
        session_start();
        $_SESSION['code']=strtolower($this->code);
    }

    //输出验证码到画布
    private function setCode(){
        for($i=0;$i<$this->len;$i++){
            $font_color = imagecolorallocate($this->img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
            $x = ($this->width/$this->len)*$i+5;
            $fontSize = mt_rand(18,28);
            $fontHeight=imagefontheight($fontSize);
            $y=  mt_rand($fontHeight+5,$this->height);
            imagettftext($this->img, $fontSize, mt_rand(-25,25), $x, $y, $font_color, $this->font_dir,$this->code[$i]);
        }
    }

    //画干扰线
    private function setLine(){
        for ($i=0; $i <$this->len ; $i++) { 
            $line_color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            imageline($this->img, mt_rand(0,$this->width/$this->len), mt_rand(0,$this->height), mt_rand($this->width-$this->width/$this->len,$this->width),  mt_rand(0,$this->height), $line_color);
        }
    }

    //画干扰点到画布
    private function setDot(){
        for ($i=0; $i < $this->width ; $i++) { 
            $dot_color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
            imagesetpixel($this->img, mt_rand(0,$this->width), mt_rand(0,$this->height), $dot_color);
        }
    }

    //输出成图像
    private function setBg(){
        ob_end_clean();
        header('content-type:image/jpeg');
        imagejpeg($this->img);
    }

    public function __destruct(){
        imagedestroy($this->img);
    }
}

//输出验证码
$captcha = new Captcha();
$captcha->entry();

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI