本文實例講述了PHP封裝的驗證碼工具類定義與用法。分享給大家供大家參考,具體如下:
下面分享的是我自己封裝的驗證碼工具類,在平時的項目中會比較經(jīng)常用到的工具類,目前封裝的這個工具類簡易版的,如果有需要的伙伴可以拿去用,當然我建議用之前在配置文件里增加一些選項信息
//驗證碼寬度 private $width; //驗證碼高度 private $height; //驗證的個數(shù) private $length; //干擾點個數(shù) private $dots; //干擾點的類型 private $type; //干擾線個數(shù) private $lines; //文字 private $font;
方便在項目中對驗證碼的要求進行更改,以方便項目邏輯的需求,而且驗證碼所選用的字體需要和驗證碼工具類放在同一目錄下,否則就要在配置文件中修改字體的路徑才能實現(xiàn)驗證碼的顯示
<?php //創(chuàng)建驗證碼工具類 class captcha { //驗證碼的各種參數(shù) //驗證碼寬度 private $width; //驗證碼高度 private $height; //驗證的個數(shù) private $length; //干擾點個數(shù) private $dots; //干擾點的類型 private $type; //干擾線個數(shù) private $lines; //文字 private $font; //驗證碼屬性的構造方法 public function __construct($arr = array ()) { //將屬性賦值 $this->width = isset ($arr['width']) ? trim($arr['width']) : '270'; $this->height = isset ($arr['height']) ? trim($arr['height']) : '30'; $this->length = isset ($arr['length']) ? trim($arr['length']) : '4'; $this->dots = isset ($arr['dots']) ? trim($arr['dots']) : '81'; $this->type = isset ($arr['type']) ? trim($arr['type']) : '*'; $this->lines = isset ($arr['lines']) ? trim($arr['lines']) : '5'; $this->font = isset ($arr['font']) ? trim($arr['font']) : './cambriab.ttf'; } //創(chuàng)建驗證碼的方法 public function captcha() { //創(chuàng)建畫布 $img = imagecreatetruecolor($this->width, $this->height); //填充顏色 //顏色資源 $color = imagecolorallocate($img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); //填充背景 imagefill($img, 0, 0, $color); //添加干擾點 for ($i = 0; $i < $this->dots; $i++) { //顏色資源 $dots_color = imagecolorallocate($img, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200)); //插入干擾點 imagestring($img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), $this->type, $dots_color); } //添加干擾線 for ($i = 0; $i < $this->lines; $i++) { //顏色資源 $line_color = imagecolorallocate($img, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200)); //插入干擾線 imageline($img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $line_color); } //首先獲取驗證碼,然后插入驗證文字 //文字高度 $size = mt_rand(18, 20); //獲取驗證碼 $str = $this->captchastring(); for ($i = 0; $i < strlen($str); $i++) { //顏色資源 $str_color = imagecolorallocate($img, mt_rand(50, 150), mt_rand(50, 150), mt_rand(50, 150)); //插入驗證碼 imagettftext($img, $size, mt_rand(-45, 45), $this->width / ($this->length + 2) * ($i +1), (($this->height - $size) / 2) + $size, $str_color, $this->font, $str[$i]); } //將得到的驗證碼存入SESSION中,便于以后的驗證碼判斷 @ session_start(); $_SESSION['captcha'] = $str; //輸出圖片 header("content-type:image/png"); imagepng($img); //清除資源 imagedestroy($img); } //獲取隨機的驗證內(nèi)容:A-Z,a-z,1-9 private function captchaString() { //獲取四個隨機的字符串 $str = ""; for ($i = 0; $i < $this->length; $i++) { switch (mt_rand(1, 3)) { case 1 : $str .= chr(mt_rand(49, 57)); break; case 2 : $str .= chr(mt_rand(97, 122)); break; case 3 : $str .= chr(mt_rand(65, 90)); break; } } return $str; } //判斷驗證碼 public static function checkCaptcha($captcha) { @ session_start(); return strtoupper($captcha) === strtoupper($_SESSION['captcha']); } } //使用方法: $img = new captcha();//這里采用默認參數(shù) $img->captcha(); ?>
運行結果:
注:代碼中用到的字體為cambriab.ttf可完整實例代碼點擊此處本站下載。
希望本文所述對大家PHP程序設計有所幫助。