PHP生成動態驗證碼的類

php生成動態驗證碼的一個類。
 
<?php
  /************************************************************************
  //FILE:ImageCode
  //DONE:生成動態驗證碼類
  //DATE"2013-6-19
  /Edition www.jbxue.com 腳本學堂
  ************************************************************************/
  class ImageCode{
   private $width;           //驗證碼圖片寬度
    private $height;         //驗證碼圖片高度
    private $codeNum;       //驗證碼字符個數 
    private $checkCode;    //驗證碼字符
    private $image;       //驗證碼畫布
   /************************************************************************
   // Function:構造函數
   // Done:成員屬性初始化
   // Author:www.jbxue.com 腳本學堂
   ************************************************************************/
   function __construct($width=60,$height=20,$codeNum=4)
   { 
    $this->width = $width;
    $this->height = $height;
    $this->codeNum = $codeNum;
    $this->checkCode = $this->createCheckCode();
   }
   function showImage()
   {
    $this->getcreateImage();
    $this->outputText();
    $this->setDisturbColor();
    $this->outputImage();
   }
   function getCheckCode()
   {
    return $this->chekCode;
   }
   private function getCreateImage()
   {
    $this->image = imagecreatetruecolor($this->width,$this->height);
    $back = imagecolorallocate($this->image,255,255,255);
    $border = imagecolorallocate($this->image,255,255,255);
    imagefilledrectangle($this->image,0,0,$this->width-1,$this->height-1,$border); 
    //使用純白色填充矩形框,這裏用的話後面干擾碼失效
    /*如果想用干擾碼的話使用下面的*/
    //imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
   }
   private function createCheckCode()
   {
    for($i=0;$i<$this->codeNum;$i++)
    {
     $number = rand(0,2);
     switch($number)
     {
      case 0: $rand_number = rand(48,57); break;//數字
      case 1: $rand_number = rand(65,90);break;//大寫字母
      case 2: $rand_number = rand(97,122);break;//小寫字母
     }
     $asc = sprintf("%c",$rand_number);
     $asc_number = $asc_number.$asc;
    }
    return $asc_number;
   }
   private function setDisturbColor()//干擾嗎設置
   {
    for($i=0;$i<=100;$i++)
    {
     //$color = imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
     $color = imagecolorallocate($this->image,255,255,255);
     imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
    }
     //$color = imagecolorallocate($this->image,0,0,0);
     //imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
     
    
   }
   private function outputText()
   {
    //隨機顏色、隨機擺放、隨機字符串向圖像輸出
    for($i=0;$i<=$this->codeNum;$i++)
    {
     $bg_color = imagecolorallocate($this->image,rand(0,255),rand(0,128),rand(0,255));
     $x = floor($this->width/$this->codeNum)*$i+3;
     $y = rand(0,$this->height-15);
     imagechar($this->image,5,$x,$y,$this->checkCode[$i],$bg_color);
    }
   }
   
   private function outputImage()
   {
    if(imagetypes()&IMG_GIF)
    {
     header("Content_type:image/gif");
     imagegif($this->image);
    }
    elseif(imagetypes()&IMG_JPG)
    {
    
     header("Content-type:image/jpeg");
     imagejpeg($this->image,"",0.5);
    }
    elseif(imagetypes()&IMG_PNG)
    {
     header("Content-type:image/png");
     imagejpeg($this->image);
    }
    elseif(imagetypes()&IMG_WBMP)
    {
     header("Content-type:image/vnd.wap.wbmp");
     imagejpeg($this->image);
    }
    else
    {
     die("PHP不支持圖像創建");
    }
   }
   
   function __destruct()
   {
    imagedestroy($this->image);
   }
  }
  
  /*顯示*/
  /*******************************************************************
  session_start();
  $image = new ImageCode(60,20,4);
  $image->showImage();
  $_SESSION['ImageCode'] = $image->getCheckCode();
  *******************************************************************/
  
 ?>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章