php驗證碼類
<? /**
- 驗證碼類
- @author firerat
- @email lukai_rat@163.com
- @time Feb 16 2011 15:28
- @lastmodify Feb 16 2011 15:28 */
class verifycode{ //image source private $im; //圖片寬度 private $w; //圖片高度 private $h; //驗證字符集合 private $text; //字符數 private $length; //字體 private $font = array(); //字體大小 private $fontsize = array(); //字體顏色 private $fontcolor = array(); //字體的偏轉角度 private $angel = array(); //背景色 private $backgroundcolor = array(); //背景圖片 private $backgroundimage = array(); //坐標-x private $x ; //坐標-y private $y ; //驗證碼存放的字段 private $checkcodefield ;
/**
* 構造函數
*
*/
public function __construct(){
//圖片寬度
$this->w = 500 ;
//圖片高度
$this->h = 600;
//驗證字符
$this->text = '0123456789qwertyuiopasdfghjklzxcvbnm';
//字體
$this->font = array(
'C:\\WINDOWS\\Fonts\\Ming Imperial.TTF',
'C:\\WINDOWS\\Fonts\\latha.TTF',
'C:\\WINDOWS\\Fonts\\ARIALNBI.TTF',
'C:\\WINDOWS\\Fonts\\GOTHICBI.TTF'
);
//字符數
$this->length = '4';
//字體大小
$this->fontsize = array(50,60,70);
//字體顏色
$this->fontcolor = array(
array('red'=>0x14, 'green'=>0x37,'blue'=>0xad),
array('red'=>0x6e, 'green'=>0x86,'blue'=>0xd6),
array('red'=>0x2c, 'green'=>0x40 ,'blue'=>0x81),
array('red'=>0x06, 'green'=>0x1f ,'blue'=>0x70),
array('red'=>0x14, 'green'=>0x37 ,'blue'=>0xad),
array('red'=>0x57, 'green'=>0x79 ,'blue'=>0xc0)
);
//字體的偏轉角度
$this->angel = array(2,4,8,-2,-4,-8);
//背景色
$this->backgroundcolor = array(
array('red'=>0x14, 'green'=>0x37,'blue'=>0xad),
array('red'=>0x6e, 'green'=>0x86,'blue'=>0xd6),
array('red'=>0x2c, 'green'=>0x40 ,'blue'=>0x81),
array('red'=>0x06, 'green'=>0x1f ,'blue'=>0x70),
array('red'=>0x14, 'green'=>0x37 ,'blue'=>0xad),
array('red'=>0x57, 'green'=>0x79 ,'blue'=>0xc0)
);
//背景圖片
$this->backgroundimage = array(
'F:\\city_photo\\city_photo\\1\\1807141.jpg',
'F:\\city_photo\\city_photo\\1\\1807141.jpg',
'F:\\city_photo\\city_photo\\1\\1807141.jpg',
'F:\\city_photo\\city_photo\\1\\1807141.jpg',
'F:\\city_photo\\city_photo\\1\\1807141.jpg'
);
//坐標軸X
$this->x = 100 ;
//坐標軸Y
$this->y = 300 ;
//驗證碼存放的字段
$this->checkcodefield = 'checkcode';
}
/* 創建背景
*
* @return $im
*/
public function createImgae(){
//$this->im = imagecreatetruecolor($this->w,$this->h);
$key = array_rand($this->backgroundimage);
$this->im = imagecreatefromjpeg( $this->backgroundimage[$key]);
return $this->im;
}
/* 創建背景的索引色
*
* @return
*/
public function createBackgroundColor(){
//獲取背景隨機顏色組key
$rgbGroupKey = array_rand($this->backgroundcolor);
$red = $this->backgroundcolor[$rgbGroupKey]['red'];
$green = $this->backgroundcolor[$rgbGroupKey]['green'];
$blue = $this->backgroundcolor[$rgbGroupKey]['blue'];
//返回顏色索引
return imagecolorallocate($this->im, $red, $green, $blue);
}
/* 獲取隨機字符,并將字符存放發到
*
* @return
*/
public function getRandStr(){
$randChars = '';
for($i = 0 ; $i < $this->length ; $i++){
$randChars .= $this->text[rand(0,strlen($this->text))] ;
}
//字體編碼統一轉換為utf-8
//$randChars = iconv("GB2312","UTF-8",$randChars);
//寫進session
$_SESSION[$this->checkcodefield] = $randChars;
return $randChars;
}
/* 創建字體顏色索引
* @return
*/
public function createFontColor(){
//獲取背景隨機顏色組key
$rgbGroupKey = array_rand($this->fontcolor);
$red = $this->fontcolor[$rgbGroupKey]['red'];
$green = $this->fontcolor[$rgbGroupKey]['green'];
$blue = $this->fontcolor[$rgbGroupKey]['blue'];
//顏色索引
return imagecolorallocate($this->im, $red, $green, $blue);;
}
//添加文字到圖片
public function addTextToImage(){
//字體顏色
$fontcolor =$this->createFontColor();
//字體
$key = array_rand($this->font);
$font = $this->font[$key];
//驗證碼
$text = $this->getRandStr();
//偏轉角度
$key = array_rand($this->angel);
$_angle = $this->angel[$key];
//起始坐標
$x = $this->x;
$y = $this->y;
//字體大小
$key = array_rand($this->fontsize);
$_fontsize = $this->fontsize[$key];
//添加文字到圖片
imagettftext($this->im, $_fontsize , $_angle, $x, $y, $fontcolor, $font, $text);
}
/**
* 輸出圖片
*
*/
public function outputImage(){
//創建布景
$this->createImgae();
//顏色添加到布景
$this->createBackgroundColor();
//添加文字到圖片
$this->addTextToImage();
//增加過期,30分鐘后過期
$expireTime = date("M, d Y H:i:s",time()+1800);
header("Expires: {$expireTime} GMT ");
header("Last-Modified: " . gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
//聲明輸出文件的類型
header('Content-type: image/png');
//輸出png圖片
imagepng($this->im);
//釋放與此圖片關聯的內存
imagedestroy($this->im);
}
}
$instance = new verifycode;
$instance->outputImage();</pre>
驗證碼效果圖:
本文由用戶 openocode 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!