zoukankan      html  css  js  c++  java
  • php面向对象(OOP)---- 验证码类

    验证码是众多网站登陆、注册等相关功能不可以或缺的功能,实现展示验证码的方式有很多,这篇文章作者以工作中比较常用的方法进行了封装。

    逻辑准备

    要实现一个完整的验证码,需要包含以下属性和功能

    验证码类
    {
      属性:
        	宽度
        	高度
        	干扰点
        	验证码(私有的)
        	验证码字符的个数
        	验证码的类型(纯数字类型、纯字母类型、混合类型)
        
        	图片类型(jpg、png、bmp……)
        	图片资源
        
       功能:
        	构造函数(用来初始化)
        	生成验证码的函数
        
        	创建画布
        	填充背景色
        	填充干扰点
        	填充干扰弧线
        	画验证码
        	输出图片
    }
    

    根据上面的逻辑,接下来我们就一步步将文字描述转换为代码

    //创建一个名为Verify的验证码类
    class Verify
    {	
      	/*
    	  定义验证码属性  	
      	 */
        //图片的宽度
      	public  $width;
      	//图片的高度
      	public  $height;
     	//私有化验证码字符串,避免生成后被修改    
    	private $verifyCode;
      	//存储验证码字符的个数
      	public  $verifyNums;
      	//存储验证码的字符类型 1->纯数字 2->纯字母 3->混合类
      	public  $verifyType;
      	//背景颜色
      	public  $bgColor;
      	//文字颜色
      	public  $fontColor;
      	//验证码的图片类型jpg,png,bmp……
      	public  $imgType;
      
      	//图片资源
      	private  $res;
    
     	/*
     		功能
     	*/
      	//功能函数,初始化一些可以被初始化的参数
      	public function __construct($width = 100,$height = 50,$imgType = 'jpg',$verifyNums = 4,$verifyType = 1)
          {
            $this->width 	  = $width;
          	$this->height     = $height;
          	$this->imgType    = $imgType;
          	$this->verifyNums = $verifyNums;
          	$this->verifyType = $verifyType;
          
          //初始化一个可以随机生成验证码的函数,将生成的验证码春初到verifyCode属性里
          	$this->verifyCode = $this->createVerifyCode(); 
          }
      	
      	//随机生成验证码的函数,因为不对外公布,设置为私有的
      	private function createVerifyCode()
          {
            //通过判断验证的类型来确定生成哪一种验证码
          	//verifyType=1生成纯数字,为2生成纯字母,为3生成混合
          	switch ($this->verifyType) {
              case 1:
                  /*生成纯数字,首先使用range(0,9)生成数组
                   *通过$this->verifyNums确定字符串的个数
                   *使用array_rand()从数组中随机获取相应个数
                   *使用join将数字拼接成字符串,存储到$str中
                   */
     				$str = join('',array_rand(range(0,9),$this->verifyNums));
                break;
              case 2:
                  /*生成纯字母,
                   *小写字母数组range('a','z')
                   *大写字母数组range('A','Z')
                   *合并两个数组array_merge()
                   *更换键和值  array_filp()
                   *随机获取数组中的特定个数的元素 array_rand()
                   *拼接成字符串 implode()
                   */
                	$str = implode(array_rand(array_filp(array_merge(range('a','z'),range('A','Z'))),$this->verifyNums));
                break;
              case 3:
                //混合类型
                $words = str_shuffle('abcdefghjkmpopqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY3456789');
                $str = substr($words,0,this->verifyNums);
                break;
          	}
          return $str;
          }
      
      
      	//开始准备生成图片
      	/*方法名:show()
      	 *功能  :调用生成图片的所有方法
      	 */
      	public function show()
          {
            $this->createImg();//创建图片资源
          	$this->fillBg();   //填充背景颜色
          	$this->fillPix();  //填充干扰点
          	$this->fillArc();  //填充干扰弧线
          	$this->writeFont();//写字
          	$this->outPutImg();//输出图片
          }
      
      	//创建图片资源:imagecreatetruecolor($width,$height)
      	private function createImg()
          {
          $this->res = imagecreatetruecolor($this->width,$this->height);
          }
      
      	//填充背景颜色:imagefill($res,$x,$y,$color)
      	//随机生成深色--->imagecolorallocate($res,$r,$g,$b)
      	private function setDarkColor()
          {
            return imagecolorallocate($this->res,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));
          }	
      	//随机生成浅色
      	private function setLightColor()
          {
            return imagecolorallocate($this->res,mt_rand(0,130),mt_rand(0,130),mt_rand(0,130));
          }
      	//开始填充
      	private function fillBg
          {
            imagefill($this->res,0,0$this->setDarkColor());
          }
      
      	//随机生成干扰点-->imagesetpixel
      	private function fillPix()
          {
            //计算产生多少个干扰点,这里设置每20个像素产生一个
          	$num = ceil(($this->width * $this->height) / 20);
          	for($i = 0; $i < $num; $i++){
              imagesetpixel($this->res,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->setDarkColor());
          	}
          }
      
      	//随机画10条弧线->imagearc()
      	private function fillArc()
          {
            for($i = 0;$i < 10;$i++){
              imagearc($this->res,
                       mt_rand(10,$this->width-10),
                       mt_rand(5,$this->height-5),
                       mt_rand(0,$this->width),
                       mt_rand(0,$this->height),
                       mt_rand(0,180),
                       mt_rand(181,360),
                       $this->setDarkColor());
            }
          }
      
      	/*在画布上写文字
    	 *根据字符的个数,将画布横向分成相应的块
    	  $every = ceil($this->width/$this->verifyNums);
    	 *每一个小块的随机位置画上对应的字符
    	  imagechar();
    	 */
      	
      	private function writeFont()
          {
          	$every = ceil($this->width / $this->verifyNums);
          	for($i = 0;$i < $this->verifyNums;$i++){
              $x = mt_rand(($every * $i) + 5,$every * ($i + 1) - 5);
              $y = mt_rand(5,$this->height - 10);
              
              imagechar($this->res,6,$x,$y,$this->verifyCode[$i],$this->setLightColor());
          	}
          }
      
      	//输出图片资源
      	private function outPutImg()
          {
          	//header('Content-type:image/图片类型')
            header('Content-type:image/'.$this->imgType);
          
          	//根据图片类型,调用不同的方法输出图片			
          	//imagepng($img)/imagejpg($img)
           	$func = 'image'.$this->imgType;
          	$func($this->res);
          }
      
      	//设置验证码字符只能调用,不能修改,用来验证验证码书否输入正确
      	public function __get($name){
          if($name = 'verifyCode'){
            return $this->verifyCode;
          }
      	}
      	
      	//析构方法,自动销毁图片资源
      	public function __destruct()
          {
          	imagedestroy($this->res);
          }
    }
    

    致此,验证码函数类已经全部完成,调用方法如下

    // Verify(宽,高,类型,个数,类型)
    // 数字类型
    $verify = new Verify(100,40,'png',4,1);
    //生成
    $verify->show();
    

    数字.jpg

    //字母类型
    $verify = new Verify(100,40,'png',4,2);
    $verify->show();
    

    字母.jpg

    //混合类型
    $verify = new Verify(100,40,'png',4,3);
    $verify->show();
    

    混合.jpg

    类中的关于创建画布的函数不明白的自行查询PHP手册

    关于匹配验证码,通过$this->verifyCode获取到字符串的值,进而进行比较


    欢迎关注微信公众号:zhyunfe-com,共同交流

  • 相关阅读:
    adodb.stream文件操作类详解
    Html中Label标记的作用和使用介绍
    正则表达式的威力轻松消除HTML代码
    只需一行代码就能让IE 6崩溃
    码农干货系列【17】Wind.js与Promise.js
    码农干货系列【3】割绳子(cut the rope)制作点滴:旋转(rotation)
    HTML5 Canvas开发者和读者的福音
    码农干货系列【8】世界上最简单的3D渲染(no webgl)
    码农干货系列【18】getting started with Promise.js(总)
    ProgressForm
  • 原文地址:https://www.cnblogs.com/zhyunfe/p/6382850.html
Copyright © 2011-2022 走看看