-
// ------------------------------ ------------------------------------------
- // 驗證碼類,該類別的物件能動態取得驗證碼圖片,驗證碼字元儲存在SESSION['code']中
- // --------------------- -------------------------------------------------- -
- // 支援4種格式數字字母漢字混合
- // ------------------------------- -----------------------------------------
- // @Author: HelloChina (sanzi0930@163.com)
- // -------------------------------------- ----------------------------------
- // @Date: 2012年6月7日11: 03:00
- // ------------------------------------------ ------------------------------
- // @version 1.0
- // ------- -------------------------------------------------- ---------------
-
- class Vcode{
- protected $width; //驗證碼寬度
- protected $height; //驗證碼長度
- protected $codeNum; //驗證碼字元數
- protected $codeType; //驗證碼類型
- protected $fontSize; //字元大小
- protected $fontType; //字型類型
- protected $codeStr; //中文內容
- protected $strNum; //中文個數
- protected $imageType; //輸出圖片類型
- protected $image; //圖片資源
- protected $checkCode; / /驗證碼內容
- /**
- ----------------------------------------------- ---------------------------------
- * 取得驗證碼資訊
- ------ -------------------------------------------------- ------------------------
- * @param integer $width 驗證碼寬度
- * @param integer $height 驗證碼高度
- * @param integer $codeNum 驗證碼字元數
- * @param integer $codeType 驗證碼字元類型1為數字2為字母3為漢字4為混編
- * @param integer $fontSize 驗證碼字體的大小
- * @param string $fontType 驗證碼字體類型
- * @param string $imageType 驗證碼輸出圖片類型
- * @param string $codestr 中文驗證碼內容
- ----- -------------------------------------------------- -------------------------
- */
- public function __construct($width=100, $height=50, $codeNum=4, $codeType=4, $fontSize=12, $fontType= 'heiti.ttf' ,$imageType='jpeg', $codeStr='去我餓人他一哦平啊是的飛個好就看了在想才吧你嗎'){
- $this-> width = $width;
- $this->height = $height;
- $this->codeNum = $codeNum;
- $this->codeType = $codeType;
- $this->fontSize = $fontSize;
- $this->fontType = $fontType;
- $this->codeStr = $codeStr;
- $this->strNum = strlen($this->codeStr)/3-1;
- $this->imageType = $imageType;
- $this->checkCode = $this->getCheckCode();
- }
-
- // ---------- -------------------------------------------------- --------------------
- //* 產生驗證碼字元
- // --------------- -------------------------------------------------- ---------------
- //* @return string
- // --------------------- -------------------------------------------------- ---------
- public function __toString(){
- $string = implode('', $this->getCheckCode());
- $_SESSION["code"]=$ string; //加到session
- $this->getImage(); //輸出驗證碼
- return '';
- }
- protected function getCheckCode(){
- $string = array();
- switch($this->codeType){
- case 1:
- //數字字串
- $string = array_rand(range(0,9), $this->codeNum );
- break;
- case 2:
- //大字母字串
- $string = array_rand(array_flip(range('A', 'Z')), $this->codeNum) ;
- break;
- case 3:
- //漢字字串
-
- for($i=0; $icodeNum); $i ){
- $start = mt_rand(0, $this->strNum);
- $string[$i]= self::msubstr($this->codeStr,$start);
- }
- break;
- case 4:
- //混合字串
- for($i=0; $icodeNum); $i ){
- $rand= mt_rand(0,2);
- switch($rand){
- case 0:
- $ascii = mt_rand(48,57);
- $string[$i] = sprintf('%c ',$ascii);
- break;
複製程式碼
|