天天看點

php輸出驗證碼圖像

咱們接着上一篇《php生成驗證碼字元串》繼續來說。

首先,我們要知道從哪幾個步驟來做

public function outImage(){
      //建立畫布
      $this->createImage();
      //填充背景色
      $this->fillBack();
      //将驗證碼字元串放到畫布上
      $this->drawChar(); 
      //添加幹擾元素
      $this->drawDisturb();
      //輸出并顯示
      $this->show();
    }

           

第一、建立畫布

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,255), mt_rand(130,255), mt_rand(130,255));
    }
           

第三、将驗證碼字元串放到畫布上

protected function drawChar(){
     $width=ceil($this->width/$this->number);
     for($i=0;$i<$this->number;$i++){
      $x=mt_rand($i*$width-5,($i+1)*$width-5);
      $y=mt_rand(0,$this->height-15);
      imagechar($this->image,5, $x, $y, $this->code[$i], $this->dackColor());
     }
    }
    protected function dackColor(){
      return imagecolorallocate($this->image, mt_rand(0,120), mt_rand(0,120), mt_rand(0,120));
    }
           

第四、添加幹擾元素

protected function drawDisturb(){
      for ($i=0; $i <150 ; $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);
    }
           

最後完整代碼總結

<?php
header("content-type:text/html;charset=utf-8"); 
$code=new Code();
$code->outImage();
  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();
  		// echo $this->code;

  	}
    public function __destruct(){
      imagedestroy($this->image);
    }
    public function __get($name){
      if($name=='code'){
        return $this->code;
      }
      return false;
    }
  	protected function createCode(){
  		//通過驗證碼類型,生成不同類型驗證碼
  		switch ($this->codeType) {
  			case 0://純數字
  				$code=$this->getNumnerCode();
  				break;
  			case 1://純字母
  				$code=$this->getCharCode();
  				break;
  			case 2://數字字幕組合:
  				$code=$this->getNumCharCode();
  				break;
  			default:
  				die('不支援這種驗證碼類型');
  		}
  		return $code;
  	}
  	protected function getNumnerCode(){
  		$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(){
  		$strNum=join('',range(0, 9));
  		$strCHar=join('',range('a', 'z'));
  		$str=$strNum.strtoupper($strCHar);
  		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,255), mt_rand(130,255), mt_rand(130,255));
    }
    protected function dackColor(){
      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-5,($i+1)*$width-5);
      $y=mt_rand(0,$this->height-15);
      imagechar($this->image,5, $x, $y, $this->code[$i], $this->dackColor());
     }
    }
    protected function drawDisturb(){
      for ($i=0; $i <150 ; $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();
    }

  }
           

運作結果

php輸出驗證碼圖像
php輸出驗證碼圖像