PHP验证码类-创新互联

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();

创新互联不只是一家网站建设的网络公司;我们对营销、技术、服务都有自己独特见解,公司采取“创意+综合+营销”一体化的方式为您提供更专业的服务!我们经历的每一步也许不一定是最完美的,但每一步都有值得深思的意义。我们珍视每一份信任,关注我们的成都网站设计、成都做网站质量和服务品质,在得到用户满意的同时,也能得到同行业的专业认可,能够为行业创新发展助力。未来将继续专注于技术创新,服务升级,满足企业一站式成都营销网站建设需求,让再小的品牌网站制作也能产生价值!

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站标题:PHP验证码类-创新互联
本文网址:http://pwwzsj.com/article/djhojd.html