php的验证码类
admin
2023-02-11 14:20:10
0

class Verify
{
    //宽
    protected $width;
    //高
    protected $height;
    //图片类型
    protected $imgType;
    //文字类型
    protected $codeType;
    //文字的个数
    protected $num;
    //保存的验证码字符串
    protected $verifyCode;
    //保存验证码资源的一个成员属性
    protected $res;
    
    
    //初始化成员
    //上面这些参数
    public function __construct($width = 100, $height = 50, $imgType = 'png', $codeType = 3, $num = 4)
    {
        $this->width = $width;
        $this->height = $height;
        $this->imgType = $imgType;
        $this->codeType = $codeType;
        $this->num = $num;
        $this->verifyCode = $this->createVerifyCode();
        
    }
    
    protected function  createVerifyCode()
    {
        $string = '';
        
        switch ($this->codeType) {
            case 1:
                $string = implode('',array_rand(range(0,9),$this->num));
                break;
            case 2:
                $string = join('',array_rand(array_flip(range('a','z')),4));
                break;
            case 3:
                /*
                for ($i = 0; $i < $this->num; $i++) {
                    $r= mt_rand(0,2);
                    switch ($r) {
                        case 0:
                            $ascii = mt_rand(48,57);
                            break;
                        case 1:
                            $ascii = mt_rand(65,90);
                            break;
                        case 2:
                            $ascii = mt_rand(97,122);
                            break;
                    }
                    $string .= chr($ascii);
                    
                }
                */
                $str = 'abcdefghijkmnpqrstuvwxzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
                $string = substr(str_shuffle($str),0,$this->num);
                break;
        }
        
        return $string;
    }
    
    
    
    //调验证码显示的一个方法 output
    //1.画图
    //2.分配颜色(写两个成员方法,调的时候直接调对应的成员方法即可)
    //3.背景填充
    //4.画干扰点
    //5.画干扰线
    //6.写字
    //7. 输出类型
    //8. 输出图片
    public function outImg()
    {
        $this->createImg();
        $this->fillBgColor();
        $this->fillPix();
        $this->fillArc();
        $this->writeFont();
        $this->output();
    }
    
    protected function output()
    {
        //p_w_picpathpng
        $func = 'p_w_picpath'.$this->imgType;
        $mime = 'Content-type:p_w_picpath/'.$this->imgType;
        header($mime);
        $func($this->res);
    }
    
    protected function writeFont()
    {
        for ($i = 0; $i < $this->num; $i++) {
            
            $width = ceil($this->width / $this->num);
            $x = $width * $i;
            $y= mt_rand(5,$this->height - 10);
            $c = $this->verifyCode[$i];
            p_w_picpathchar($this->res,5,$x,$y,$c,$this->darkColor());
        }
        
    }
    
    protected function fillArc()
    {
        for($i = 0; $i < 10; $i++) {
            p_w_picpatharc($this->res,
                    mt_rand(10,$this->width - 10),
                    mt_rand(10,$this->height - 10),
                    mt_rand(0,$this->width),
                    mt_rand(0,$this->height),
                    mt_rand(0,180),
                    mt_rand(181,360),
                    $this->lightColor()
                    );
        }
    }
    
    protected function fillPix()
    {
        $num = $this->pixNum();
        for ($i = 0; $i < $num; $i++) {
            
            p_w_picpathsetpixel($this->res,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->darkColor());
        }
    }
    
    protected function pixNum()
    {
        $area = ceil(($this->width * $this->height) / 20);
        return $area;
    }
    
    
    protected function fillBgColor()
    {
        p_w_picpathfill($this->res,0,0,$this->lightColor());
    }
    
    
    protected function lightColor()
    {
        return p_w_picpathcolorallocate($this->res,
                           mt_rand(130,255),
                           mt_rand(130,255),
                           mt_rand(130,255)
                            );
    }
    
    protected function darkColor()
    {
        return p_w_picpathcolorallocate($this->res,
                           mt_rand(0,120),
                           mt_rand(0,120),
                           mt_rand(0,120)
                            );
    }
    
    protected function createImg()
    {
        $this->res = p_w_picpathcreatetruecolor($this->width,$this->height);
    }
    
    
    //9 .销毁图片资源
    public function __destruct()
    {
        //p_w_picpathdestroy($this->res);
    }
    
    //可以做一个魔术方法__get专门用于得到验证码字符串
    public function __get($key)
    {
        if ($key == 'verifyCode') {
            return $this->$key;
        }
        
        return false;
    }
    
    
}

相关内容

热门资讯

澳大利亚工党大会上,中方官员愤... 据澳大利亚广播公司(ABC)7月23日报道,中国外交官因不满台湾地区代表出席澳大利亚工党(ALP)在...
裸体在家犯法还是拍别人裸照犯法... 近日,#男子在家裸体被邻居拍照发群#一事引发热议。据报道,某小区一名男业主在家中未穿衣物,对面女业主...
“每天跟遛狗一样”,万亿资金,... 进入7月,市场波动加大,A股关于量化交易的争议再次达到顶峰。由于散户与量化机构之间的“体感温差”显著...
特朗普:对伊朗战争进展顺利 当地时间7月23日,美国总统特朗普再次称,“美国在对抗伊朗方面进展很顺利,表现得极其出色,好得超乎任...
国务院成立广西六蓝水库溃坝灾害... 7月6日,广西南宁横州市六蓝水库发生溃坝,造成重大人员伤亡。根据国家有关法律法规规定,国务院成立调查...
倚美搞事,菲律宾政府连“为奴”... 南海的风浪,很大一部分是菲律宾政府自己搅起来的。近年来,菲政府在仁爱礁、黄岩岛等地小动作频频,不仅频...
雨花台区企业摘得国家科学技术进... 晨报讯(南京晨报/爱南京记者 陈彦)记者从南京市雨花台区获悉,在新近揭晓的2025年度国家科学技术奖...
美国公布新关税措施,日本、澳大... 据法新社、澳大利亚广播公司(ABC)报道,针对美国将于美东时间24日以所谓“强迫劳动”为名对数十个国...
济南大数据集团激活数据要素潜能... 走进“泉数汇·数港”线下体验空间,健康画像、求职背调、“泉涌”工作站等数据产品一字排开,来访者可以像...
太保寿险河南分公司举办员工家庭... 5月22日,中国太保寿险河南分公司在郑州举办绩优干部员工家庭开放日活动,邀请员工家属走进企业,参观办...