最新最全PHP生成制作驗(yàn)證碼代碼詳解(推薦)
1.0 首先先看代碼
<?php header("Content-Type:text/html;Charset=UTF-");// 設(shè)置頁(yè)面的編碼風(fēng)格 header("Content-Type:image/jpeg");// 通知瀏覽器輸出的是jpeg格式的圖像 $img = imagecreatetruecolor(,);//創(chuàng)建畫布并設(shè)置大小 x軸 y軸 $bgcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));//分配背景顏色 imagefill($img, , , $bgcolor); ////把背景填充到圖像 imagejpeg($img); // 輸出圖像 imagedestroy($img); // 銷毀圖像 ?>
好,現(xiàn)在結(jié)合以上代碼,來(lái)分析分析以上用到的幾個(gè)函數(shù):
① imagecreatetruecolor();
imagecreatetruecolor — 新建一個(gè)真彩色圖像(感覺(jué)哇,那么長(zhǎng),其實(shí)仔細(xì)一看挺好記的 image/create/true/color,什么是真彩色圖像?往下看)
resource imagecreatetruecolor ( int $width , int $height )
imagecreatetruecolor() 和 imagecreate()兩個(gè)函數(shù)都能創(chuàng)建畫布
resource imagecreate ( int $x_size , int $y_size )
imagecreatetruecolor()建立的是一幅大小為 x和 y的黑色圖像(默認(rèn)為黑色[即便叫法就是真彩色圖像]),如想改變背景顏色則需
要用填充顏色函數(shù) imagefill($img,0,0,$color);
imagecreate 新建一個(gè)空白圖像資源,用imagecolorAllocate()添加背景色
上面兩個(gè)函數(shù)只不過(guò)是一個(gè)功能的兩種方法
② imagecolorallocate();
imagecolorallocate — 為一幅圖像分配顏色
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
顏色分別用 紅 綠 藍(lán)三色組合,這些參數(shù)是 0 到 255 的整數(shù)或者十六進(jìn)制的 0x00 到 0xFF。
③ mt_rand();
mt_rand — 生成更好的隨機(jī)數(shù)
int mt_rand ( int $min , int $max )
$min 可選的、返回的最小值(默認(rèn):0) $max 可選的、返回的最大值(默認(rèn):mt_getrandmax())
這里就是用來(lái)讓他隨機(jī)生成背景顏色,0-255隨便取值。所以頁(yè)面沒(méi)刷新一次畫布背景顏色就不一樣。效果圖:
2.0 開(kāi)始往里面做干擾線,干擾點(diǎn)。防止驗(yàn)證圖像被秒識(shí)別
<?php header("Content-Type:text/html;Charset=UTF-");// 設(shè)置頁(yè)面的編碼風(fēng)格 header("Content-Type:image/jpeg");// 通知瀏覽器輸出的是jpeg格式的圖像 $img = imagecreatetruecolor(,);//創(chuàng)建畫布并設(shè)置大小 x軸 y軸 $bgcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));//分配背景顏色 //添加干擾線,并循環(huán)次,背景顏色隨機(jī) for($i=;$i<;$i++){ $linecolor = imagecolorallocate($img,mt_rand(,),mt_rand(,),mt_rand(,)); imageline($img, mt_rand(,), mt_rand(,), mt_rand(,), mt_rand(,), $linecolor); } //添加干擾點(diǎn),并循環(huán)次,背景顏色隨機(jī) for($i=;$i<;$i++){ $dotcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,)); imagesetpixel($img, mt_rand(,), mt_rand(,), $dotcolor); } imagefill($img, , , $bgcolor); ////把背景填充到圖像 imagejpeg($img); // 輸出圖像 imagedestroy($img); // 銷毀圖像 ?>
函數(shù)分析:
① imageline();
imageline — 畫一條線段
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline() 用 color 顏色在圖像 image 中從坐標(biāo) x1,y1 到 x2,y2(圖像左上角為 0, 0)畫一條線段。
imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);這里意思就是 畫布$img 中從坐標(biāo) x1,y1 到 x2,y2隨機(jī)
② imagesetpixel();
imagesetpixel— 畫一個(gè)單一像素
bool imagesetpixel ( resource $image , int $x , int $y , int $color )
imagesetpixel() 在 image 圖像中用 color 顏色在 x,y 坐標(biāo)(圖像左上角為 0,0)上畫一個(gè)點(diǎn)。
imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);具體含義同上
效果圖:
3.0 添加驗(yàn)證字母數(shù)字
<?php header("Content-Type:text/html;Charset=UTF-");// 設(shè)置頁(yè)面的編碼風(fēng)格 header("Content-Type:image/jpeg");// 通知瀏覽器輸出的是jpeg格式的圖像 $img = imagecreatetruecolor(,);//創(chuàng)建畫布并設(shè)置大小 x軸 y軸 $bgcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));//分配背景顏色 //添加干擾線,并循環(huán)次,背景顏色隨機(jī) for($i=;$i<;$i++){ $linecolor = imagecolorallocate($img,mt_rand(,),mt_rand(,),mt_rand(,)); imageline($img, mt_rand(,), mt_rand(,), mt_rand(,), mt_rand(,), $linecolor); } //添加干擾點(diǎn),并循環(huán)次,背景顏色隨機(jī) for($i=;$i<;$i++){ $dotcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,)); imagesetpixel($img, mt_rand(,), mt_rand(,), $dotcolor); } //添加需要驗(yàn)證的字母或者數(shù)字 $rand_str = "qwertyuiopasdfghjklzxcvbnm";//需要使用到驗(yàn)證的一些字母和數(shù)字 $str_arr = array(); //命名一個(gè)數(shù)組 for($i = ;$i<;$i++){ //循環(huán)次,就是有四個(gè)隨機(jī)的字母或者數(shù)字 $pos = mt_rand(,strlen($rand_str)-); $str_arr[] = $rand_str[$pos];//臨時(shí)交換 } $x_start=/;//單個(gè)字符X軸位置 foreach ($str_arr as $key) { $fontcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,)); imagettftext($img, , mt_rand(-,), $x_start, /, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key); $x_start +=;//遍歷后單個(gè)字符沿X軸 + } imagefill($img, , , $bgcolor); ////把背景填充到圖像 imagejpeg($img); // 輸出圖像 imagedestroy($img); // 銷毀圖像 ?>
函數(shù):
imagettftext();
imagettftext — 用 TrueType 字體向圖像寫入文本
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
分析下面的代碼:
imagettftext($img, 25, mt_rand(-15,15), $x_start, 50/2, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key);
$img-----------畫布
25-----------字體的尺寸。
mt_rand(-15,15)----------角度制表示的角度,0 度為從左向右讀的文本。更高數(shù)值表示逆時(shí)針旋轉(zhuǎn)。例如 90 度表示從下向上讀的文本。(就是字體角度的問(wèn)題,)
$x_start----------通俗易懂的講就是字符的X軸位置
50/2----------字符的高度
$fontcolor----------字符顏色
"C:/Windows/Fonts/Verdana.TTF"----------字符的字體樣式路徑
$key-----------遍歷出后的字符
效果:
以上內(nèi)容是本文給大家介紹的最新最全PHP生成制作驗(yàn)證碼代碼詳解(推薦)的全部敘述,希望對(duì)大家有所幫助!
- php圖像驗(yàn)證碼生成代碼
- PHP生成各種隨機(jī)驗(yàn)證碼的方法總結(jié)【附demo源碼】
- PHP封裝函數(shù)實(shí)現(xiàn)生成隨機(jī)的字符串驗(yàn)證碼
- PHP生成圖片驗(yàn)證碼功能示例
- PHP生成圖像驗(yàn)證碼的方法小結(jié)(2種方法)
- PHP生成制作驗(yàn)證碼的簡(jiǎn)單實(shí)例
- 基于PHP生成簡(jiǎn)單的驗(yàn)證碼
- PHP生成可點(diǎn)擊刷新的驗(yàn)證碼簡(jiǎn)單示例
- php生成酷炫的四個(gè)字符驗(yàn)證碼
- php生成圖片驗(yàn)證碼的方法
- php實(shí)現(xiàn)生成驗(yàn)證碼實(shí)例分享
- php生成復(fù)雜驗(yàn)證碼(傾斜,正弦干擾線,黏貼,旋轉(zhuǎn))
相關(guān)文章
Linux中用PHP判斷程序運(yùn)行狀態(tài)的2個(gè)方法
這篇文章主要介紹了Linux中用PHP判斷程序運(yùn)行狀態(tài)的2個(gè)方法,需要的朋友可以參考下2014-05-05Zend Framework入門之環(huán)境配置及第一個(gè)Hello World示例(附demo源碼下載)
這篇文章主要介紹了Zend Framework入門之環(huán)境配置及第一個(gè)Hello World示例,詳細(xì)講述了Zend Framework環(huán)境搭建與配置,以及實(shí)現(xiàn)第一個(gè)Hello World程序的方法,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-03-03PHP讓網(wǎng)站移動(dòng)訪問(wèn)更加友好方法
在本文里我們給大家整理了關(guān)于PHP讓網(wǎng)站移動(dòng)訪問(wèn)更加友好的相關(guān)實(shí)例代碼以及相關(guān)知識(shí)點(diǎn),有需要的朋友們學(xué)習(xí)下。2019-02-02php如何根據(jù)IP獲取當(dāng)前經(jīng)緯度以及地域信息
這篇文章主要給大家介紹了關(guān)于php如何根據(jù)IP獲取當(dāng)前經(jīng)緯度以及地域信息的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01百萬(wàn)級(jí)別知乎用戶數(shù)據(jù)抓取與分析之PHP開(kāi)發(fā)
這篇文章主要介紹了百萬(wàn)級(jí)別知乎用戶數(shù)據(jù)抓取與分析之PHP開(kāi)發(fā)的相關(guān)資料,需要的朋友可以參考下2015-09-09IIS6.0 開(kāi)啟Gzip方法及PHP Gzip函數(shù)分享
因?yàn)樵谧鲆粋€(gè)項(xiàng)目,項(xiàng)目里面服務(wù)器主要提供數(shù)據(jù),但是數(shù)據(jù)多了文件就大了,比較浪費(fèi)流量和時(shí)間,我們便用Gzip來(lái)處理。我在本機(jī)上是apache,服務(wù)器上是IIS6.0,用的是php,那么我就在這里分享一下。2014-06-06laravel5創(chuàng)建service provider和facade的方法詳解
這篇文章主要介紹了laravel5創(chuàng)建service provider和facade的方法,實(shí)例分析了laravel創(chuàng)建service、provider和facade類的具體步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07使用Laravel中的查詢構(gòu)造器實(shí)現(xiàn)增刪改查功能
這篇文章主要介紹了使用Laravel中的查詢構(gòu)造器實(shí)現(xiàn)增刪改查功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09