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;
    }
    
    
}

相关内容

热门资讯

德国总理:美国正在被伊朗羞辱 德国之声4月27日报道,德国总理默茨在访问一所学校时表示,在当前的持续冲突中,伊朗领导层正试图羞辱美...
理响中国|“长”歌以行,风云激... 光阴如梭,东方潮阔。这里是中国的长三角,世界的长三角。无论过去、现在还是未来,这片土地都因时代而生,...
白宫:特朗普及其国安团队开会讨... 新华社华盛顿4月27日电 美国白宫新闻秘书莱维特27日在记者会上证实,总统特朗普及其国家安全团队当天...
人民日报刊文:日本放开杀伤性武... 日本放开杀伤性武器出口推高地缘冲突风险(国际论坛)常思纯《人民日报》(2026年04月28日 第 0...
医疗保障法草案二审:明确生育保... 满足多样化健康保障需求本报记者 彭 波4月27日,医疗保障法草案二审稿提请十四届全国人大常委会第二十...
天津一景区发生自转旋翼机事故1... 澎湃新闻记者 吕新文中国民用航空华北地区管理局4月22日公布《豪客通航“10•1”天津长芦汉盐旅游区...
卡塔尔埃米尔与美国总统特朗普通... 当地时间24日,卡塔尔埃米尔塔米姆与美国总统特朗普通电话,重点就中东地区局势以及伊朗与美国谈判问题交...
男子30年前被扣押2859克黄... 澎湃新闻记者 王鑫家住辽宁省大连市的潘永嘉近日向澎湃新闻反映称,三十年前,他在大连周水子机场被盖州市...
商务部:取消反制欧盟两家金融机... 中华人民共和国商务部令二〇二六年 第1号鉴于欧盟已取消对中国两家金融机构的制裁措施,现公布《关于取消...
过去24小时共有5艘船只通过霍... 总台记者当地时间24日获悉,过去24小时内,共有5艘船只通过霍尔木兹海峡,其中包括一艘伊朗油轮。(总...