PHP验证码类
admin
2023-06-13 03:21:59
0
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();

相关内容

热门资讯

王毅会见巴军方高层,谈及伊朗局... 据凤凰卫视报道,中共中央政治局委员、外交部长王毅5月25日在人民大会堂会见巴基斯坦国防军司令兼陆军参...
何妨把课堂打造成学术“直播间” 评论员说 何妨把课堂打造成学术“直播间” ■ 本报评论员 刘晓庆 其实,不只是大学课堂越来越“静悄悄...
空间站完成第8次“太空会师” 5月25日在北京航天飞行控制中心飞控大厅拍摄的神舟二十一号航天员乘组和神舟二十三号航天员乘组拍摄“全...
美军称在伊朗南部实施自卫打击 央视记者获悉,美国中央司令部发言人蒂姆·霍金斯上尉表示,美军25日在伊朗南部实施了自卫打击,“旨在保...
小米押注!闪回科技四次递表港交... 【大河财立方消息】5月25日,闪回科技有限公司(以下简称闪回科技)在港交所提交上市申请,独家保荐人为...
神舟二十三号飞船发射任务圆满成... 乘组一名航天员将开展一年期在轨驻留试验 神舟二十三号飞船发射任务圆满成功 北京时间5月24日23时0...
西安警方:女孩与父亲争执被路人... 5月25日凌晨,针对网传“16岁女孩被陌生男子殴打”一事,西安市公安局莲湖分局发布警情通报。据江西广...
力鼎光电获得外观设计专利授权:... 证券之星消息,根据天眼查APP数据显示力鼎光电(605118)新获得一项外观设计专利授权,专利名为“...
“一箭8星”再启新程!中科宇航... IT之家 5 月 25 日消息,中科宇航今日宣布,近日,力箭一号遥十四运载火箭在中科宇航产业化基地如...
鄠图科普丨一起“趣”阅读|火箭... 火箭发射是太空旅行的开始, 也是人类探索宇宙的重要一步。 火箭是载人和无人航天器进入太空的必备工具,...