封装验证码

使用GD库操作的一般步骤

  1. 创建画布
  2. 创建颜色
  3. 用GD库的函数去画画
  4. 告诉浏览器你的mime类型
  5. 输出到浏览器或者保存到本地
  6. 销毁
<?php
      //width,height画布的宽和高,num是显示字符的数量,type是显现字符的类型,有3种 
        
         function verify($width=100,$height=40,$num=5,$type=3){

            //1.创建画布
            $image = imagecreatetruecolor($width, $height);

            //给矩形填充浅色
            imagefilledrectangle($image,0,0,$width,$height,lightColor($image)) ;

            //3.生成字符
            $string="";
            switch ($type) {
                case 1:
                    $str= "0123456789";
                    $string=substr(str_shuffle($str),0,$num);
                    break;
                
                case 2:
                    $arr=range("a", "z");
                    shuffle($arr);
                    $string=substr(implode("",$arr),0,$num);
                    break;

                case 3:
                    $str="0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFGDSAZXCVBNM";
                    $string=substr(str_shuffle($str), 0,$num); 
                    break;      
            }

            

            //4.开始写字
            for($i=0;$i<$num;$i++){
                $x=floor($width/$num)*$i;
                $y=mt_rand(10,$height-20);
                imagechar($image, 5, $x, $y, $string[$i], deepColor($image));
            }

            //5.干扰线(点)

            for($i=0;$i<$num;$i++){
             imagearc($image,mt_rand(10,$width),mt_rand(10,$height),mt_rand(10,$width),mt_rand(10,$height),mt_rand(0,10),mt_rand(0,270),deepColor($image));
            }

            for($i=0;$i<50;$i++){

                 imagesetpixel($image,mt_rand(0,$width),mt_rand(0,$height),deepColor($image)); 
            }

            //6.指定输出类型
                header("Content-type:image/png");


            //7.准备输出
                imagepng($image);
            //8.销毁
                imagedestroy($image);

        }

                  //2.生成浅色
        function lightColor($image){

            return imagecolorallocate($image, mt_rand(130,250), mt_rand(130,250), mt_rand(130,250));
        }

                //生成深色
        function deepColor($image){

            return imagecolorallocate($image, mt_rand(0,125), mt_rand(0,125), mt_rand(0,125));
        }


封装验证码类

class Code{
                //验证码个数
            protected $number;
            //验证码类型
            protected $codeType;
            //图像宽度
            protected $width;
            //图像高度
            protected $height;
            //图像资源
            protected $image;
            //验证码字符串
            protected $code;

            public function __construct($number=4,$codeType=2,$width=100,$height=50){
                //初始化自己的成员属性
                $this->number = $number;
                $this->codeType = $codeType;
                $this->width = $width;
                $this->height = $height;
                    //生成验证码
                $this->code = $this->createCode();
                
            }
            public function __destruct(){
                imagedestroy($this->image);
            }

            public function __get($name){
                if($name == 'code'){
                    echo $this->code;
                }else{
                    return false;
                }

            }

            protected function createCode(){
                //通过你的验证码类型生成不同的验证码
                switch($this->codeType){
                    case 0: //纯数字
                        $code = $this->getNumberCode();
                        break;
                    case 1: //纯字母
                        $code = $this->getCharCode();
                        break;
                    case 2: //数字和字母组合
                        $code = $this->getNumCharCode();
                        break;
                    default:
                    die('不支持这种验证码类型');  

                }
                return $code;

            }
            protected function getNumberCode(){
                $str = join('',range(0,9));
                return substr(str_shuffle($str), 0,$this->number);
            }

            protected function getCharCode(){
                $str = join('',range('a', 'z'));
                $str = $str.strtoupper($str);
                return substr(str_shuffle($str), 0,$this->number);

            }
            protected function getNumCharCode(){
                $numStr =join('',range(0, 9));
                $str = join('',range('a', 'z'));
                $str = $numStr.$str.strtoupper($str);
                return substr(str_shuffle($str), 0,$this->number);

            }
            protected function createImage(){
                $this->image = imagecreatetruecolor($this->width, $this->height);

            }
            protected function fillBack(){
                imagefill($this->image,0,0,$this->lightColor());
            }

            protected function lightColor(){
                return imagecolorallocate($this->image, mt_rand(130,250), mt_rand(130,250), mt_rand(130,250));
            }
            protected function darkColor(){
                return imagecolorallocate($this->image, mt_rand(0,120), mt_rand(0,120), mt_rand(0,120));
            }

            protected function drawChar(){
                $width = ceil($this->width/$this->number);
                for($i= 0;$i<$this->number;$i++){
                    $x = mt_rand($i*$width+10,($i+1)*$width-10);
                    $y= mt_rand(0,$this->height-15);
                    imagechar($this->image, 5, $x, $y, $this->code[$i], $this->darkColor());
                }
            }
            protected function drawDisturb(){
                for($i=0;$i<100;$i++){
                    $x= mt_rand(0,$this->width);
                    $y = mt_rand(0,$this->height);
                    imagesetpixel($this->image, $x, $y, $this->lightColor());
                }
            }
            protected function show(){
                header("Content-type:image/png");
                imagepng($this->image);
            }




            public function outImage(){
                //创建画布
                $this->createImage();
                //填充背景色
                $this->fillBack();
                //将验证码画到画布
                $this->drawChar();
                //添加干扰项
                $this->drawDisturb();
                //输出并显示
                $this->show();
            }

    }       



©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • 验证码是众多网站登陆、注册等相关功能不可以或缺的功能,实现展示验证码的方式有很多,这篇文章作者以工作中比较常用的方...
    Demoer阅读 745评论 7 9
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,204评论 0 17
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,569评论 25 707
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • 去年料想今日,未料今日如此快的到来,猝不及防的离别,长亭古道满是落叶。说好的送人,结果是被送人的等,新乡十一月的天...
    巷子里的猫耳阅读 180评论 0 0