驗證碼是眾多網站登陸、注冊等相關功能不可以或缺的功能,實現(xiàn)展示驗證碼的方式有很多,這篇文章作者以工作中比較常用的方法進行了封裝。
邏輯準備
要實現(xiàn)一個完整的驗證碼,需要包含以下屬性和功能
驗證碼類
{
屬性:
寬度
高度
干擾點
驗證碼(私有的)
驗證碼字符的個數(shù)
驗證碼的類型(純數(shù)字類型、純字母類型、混合類型)
圖片類型(jpg、png、bmp……)
圖片資源
功能:
構造函數(shù)(用來初始化)
生成驗證碼的函數(shù)
創(chuàng)建畫布
填充背景色
填充干擾點
填充干擾弧線
畫驗證碼
輸出圖片
}
根據上面的邏輯,接下來我們就一步步將文字描述轉換為代碼
//創(chuàng)建一個名為Verify的驗證碼類
class Verify
{
/*
定義驗證碼屬性
*/
//圖片的寬度
public $width;
//圖片的高度
public $height;
//私有化驗證碼字符串,避免生成后被修改
private $verifyCode;
//存儲驗證碼字符的個數(shù)
public $verifyNums;
//存儲驗證碼的字符類型 1->純數(shù)字 2->純字母 3->混合類
public $verifyType;
//背景顏色
public $bgColor;
//文字顏色
public $fontColor;
//驗證碼的圖片類型jpg,png,bmp……
public $imgType;
//圖片資源
private $res;
/*
功能
*/
//功能函數(shù),初始化一些可以被初始化的參數(shù)
public function __construct($width = 100,$height = 50,$imgType = 'jpg',$verifyNums = 4,$verifyType = 1)
{
$this->width = $width;
$this->height = $height;
$this->imgType = $imgType;
$this->verifyNums = $verifyNums;
$this->verifyType = $verifyType;
//初始化一個可以隨機生成驗證碼的函數(shù),將生成的驗證碼春初到verifyCode屬性里
$this->verifyCode = $this->createVerifyCode();
}
//隨機生成驗證碼的函數(shù),因為不對外公布,設置為私有的
private function createVerifyCode()
{
//通過判斷驗證的類型來確定生成哪一種驗證碼
//verifyType=1生成純數(shù)字,為2生成純字母,為3生成混合
switch ($this->verifyType) {
case 1:
/*生成純數(shù)字,首先使用range(0,9)生成數(shù)組
*通過$this->verifyNums確定字符串的個數(shù)
*使用array_rand()從數(shù)組中隨機獲取相應個數(shù)
*使用join將數(shù)字拼接成字符串,存儲到$str中
*/
$str = join('',array_rand(range(0,9),$this->verifyNums));
break;
case 2:
/*生成純字母,
*小寫字母數(shù)組range('a','z')
*大寫字母數(shù)組range('A','Z')
*合并兩個數(shù)組array_merge()
*更換鍵和值 array_filp()
*隨機獲取數(shù)組中的特定個數(shù)的元素 array_rand()
*拼接成字符串 implode()
*/
$str = implode(array_rand(array_filp(array_merge(range('a','z'),range('A','Z'))),$this->verifyNums));
break;
case 3:
//混合類型
$words = str_shuffle('abcdefghjkmpopqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY3456789');
$str = substr($words,0,this->verifyNums);
break;
}
return $str;
}
//開始準備生成圖片
/*方法名:show()
*功能 :調用生成圖片的所有方法
*/
public function show()
{
$this->createImg();//創(chuàng)建圖片資源
$this->fillBg(); //填充背景顏色
$this->fillPix(); //填充干擾點
$this->fillArc(); //填充干擾弧線
$this->writeFont();//寫字
$this->outPutImg();//輸出圖片
}
//創(chuàng)建圖片資源:imagecreatetruecolor($width,$height)
private function createImg()
{
$this->res = imagecreatetruecolor($this->width,$this->height);
}
//填充背景顏色:imagefill($res,$x,$y,$color)
//隨機生成深色--->imagecolorallocate($res,$r,$g,$b)
private function setDarkColor()
{
return imagecolorallocate($this->res,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));
}
//隨機生成淺色
private function setLightColor()
{
return imagecolorallocate($this->res,mt_rand(0,130),mt_rand(0,130),mt_rand(0,130));
}
//開始填充
private function fillBg
{
imagefill($this->res,0,0$this->setDarkColor());
}
//隨機生成干擾點-->imagesetpixel
private function fillPix()
{
//計算產生多少個干擾點,這里設置每20個像素產生一個
$num = ceil(($this->width * $this->height) / 20);
for($i = 0; $i < $num; $i++){
imagesetpixel($this->res,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->setDarkColor());
}
}
//隨機畫10條弧線->imagearc()
private function fillArc()
{
for($i = 0;$i < 10;$i++){
imagearc($this->res,
mt_rand(10,$this->width-10),
mt_rand(5,$this->height-5),
mt_rand(0,$this->width),
mt_rand(0,$this->height),
mt_rand(0,180),
mt_rand(181,360),
$this->setDarkColor());
}
}
/*在畫布上寫文字
*根據字符的個數(shù),將畫布橫向分成相應的塊
$every = ceil($this->width/$this->verifyNums);
*每一個小塊的隨機位置畫上對應的字符
imagechar();
*/
private function writeFont()
{
$every = ceil($this->width / $this->verifyNums);
for($i = 0;$i < $this->verifyNums;$i++){
$x = mt_rand(($every * $i) + 5,$every * ($i + 1) - 5);
$y = mt_rand(5,$this->height - 10);
imagechar($this->res,6,$x,$y,$this->verifyCode[$i],$this->setLightColor());
}
}
//輸出圖片資源
private function outPutImg()
{
//header('Content-type:image/圖片類型')
header('Content-type:image/'.$this->imgType);
//根據圖片類型,調用不同的方法輸出圖片
//imagepng($img)/imagejpg($img)
$func = 'image'.$this->imgType;
$func($this->res);
}
//設置驗證碼字符只能調用,不能修改,用來驗證驗證碼書否輸入正確
public function __get($name){
if($name = 'verifyCode'){
return $this->verifyCode;
}
}
//析構方法,自動銷毀圖片資源
public function __destruct()
{
imagedestroy($this->res);
}
}
致此,驗證碼函數(shù)類已經全部完成,調用方法如下
// Verify(寬,高,類型,個數(shù),類型)
// 數(shù)字類型
$verify = new Verify(100,40,'png',4,1);
//生成
$verify->show();
數(shù)字.jpg
//字母類型
$verify = new Verify(100,40,'png',4,2);
$verify->show();
字母.jpg
//混合類型
$verify = new Verify(100,40,'png',4,3);
$verify->show();
混合.jpg
類中的關于創(chuàng)建畫布的函數(shù)不明白的自行查詢PHP手冊
關于匹配驗證碼,通過$this->verifyCode獲取到字符串的值,進而進行比較
歡迎關注微信公眾號:zhyunfe-com,共同交流
zhyunfe-com.jpg