您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

tp5验证码

2025/9/11 11:17:43发布33次查看
tp5验证码
类文件位于extend/other/captcha.php
//控制器中 获取验证码
       public function get_captcha(){    
        //使用memcheck 设置session    
        session::init(['prefix'=> 'wll_','type'=> '','auto_start' => true]);
        $captcha = new \other\captcha(86,48,4);
        echo $captcha->showimg();        
        session::set('code',$captcha->getcaptcha());
        exit;    
    }
模块中:
echo session::get('code','wll_'); 输出验证码
以下是captcha.php 类
width = $width;
        $this->height = $height;
        $this->codenum = $codenum;
    }
    //显示验证码
    function showimg(){
        //创建图片
        $this->createimg();
        //设置干扰元素
        $this->setdisturb();
        //设置验证码
        $this->setcaptcha();
        //输出图片
        $this->outputimg();
    }
//获取显示的验证码,用来验证验证码是否数据正确
    function getcaptcha(){
        return $this->code;
    }
//创建图片
    private function createimg(){
        $this->im = imagecreatetruecolor($this->width, $this->height);
        $bgcolor = imagecolorallocate($this->im, 255, 255, 255);//创建的前景为白色
        imagefill($this->im, 0, 0, $bgcolor);
    }
//设置干扰元素
    private function setdisturb(){
        $area = ($this->width * $this->height) / 20;
        $disturbnum = ($area > 250) ? 250 : $area;
        //加入点干扰
        for ($i = 0; $i             $color = imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255));
            imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color);
        }
        //加入弧线
        for ($i = 0; $i             $color = imagecolorallocate($this->im, rand(128, 255), rand(125, 255), rand(100, 255));
            imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color);
        }
    }
    //设置验证码随机数
    private function createcode(){
        $str = 23456789abcdefghijkmnpqrstuvwxyzabcdefghijkmnpqrstuvwxyz;
        for ($i = 0; $i codenum; $i++) {
            $this->code .= $str{rand(0, strlen($str) - 1)};
        }
    }
    //设置验证码
    private function setcaptcha(){
//设置验证码随机数        
        $this->createcode();
//文字前景
        $color = imagecolorallocate($this->im, rand(50, 250), rand(100, 250), rand(128, 250));
//因为imagechar最大的文字字体为5,字体太小而不用这个方式了
        //imagechar($this->im, $size, $x, $y, $this->code{$i}, $color);
//因为imagechar最大的文字字体为5,而这里要显示更大的文字,所以用 imagefttext 
        imagefttext($this->im,30,0,10,35,$color,'static/pc/fonts/monofont.ttf',$this->code);//图象资源,尺寸,角度,x轴,y轴,颜色,字体路径,文本插入图像
    }
    //输出图片
    private function outputimg(){
        if (imagetypes() & img_jpg) {
            header('content-type:image/jpeg');
            imagejpeg($this->im);
        } elseif (imagetypes() & img_gif) {
            header('content-type: image/gif');
            imagegif($this->im);
        } elseif (imagetype() & img_png) {
            header('content-type: image/png');
            imagepng($this->im);
        } else {
            die(don't support image type!);
        }
    }//end
}
ad:真正免费,域名+虚机+企业邮箱=0元
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product