设为首页 收藏本站
查看: 676|回复: 0

[经验分享] php的验证码类

[复制链接]

尚未签到

发表于 2018-12-15 12:50:30 | 显示全部楼层 |阅读模式
  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()
    {
        //imagepng
        $func = 'image'.$this->imgType;
        $mime = 'Content-type:image/'.$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];
            imagechar($this->res,5,$x,$y,$c,$this->darkColor());
        }
        
    }
   
    protected function fillArc()
    {
        for($i = 0; $i < 10; $i++) {
            imagearc($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++) {
            
            imagesetpixel($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()
    {
        imagefill($this->res,0,0,$this->lightColor());
    }
   
   
    protected function lightColor()
    {
        return imagecolorallocate($this->res,
                           mt_rand(130,255),
                           mt_rand(130,255),
                           mt_rand(130,255)
                            );
    }
   
    protected function darkColor()
    {
        return imagecolorallocate($this->res,
                           mt_rand(0,120),
                           mt_rand(0,120),
                           mt_rand(0,120)
                            );
    }
   
    protected function createImg()
    {
        $this->res = imagecreatetruecolor($this->width,$this->height);
    }
   
   
    //9 .销毁图片资源
    public function __destruct()
    {
        //imagedestroy($this->res);
    }
   
    //可以做一个魔术方法__get专门用于得到验证码字符串
    public function __get($key)
    {
        if ($key == 'verifyCode') {
            return $this->$key;
        }
        
        return false;
    }
   
   
}




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-651697-1-1.html 上篇帖子: php教程 TTP中GET与POST的区别 下篇帖子: PHP中面向对象中的模板引擎类
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表