《PHP編程:PHP生成制作驗證碼的簡單實例》要點:
本文介紹了PHP編程:PHP生成制作驗證碼的簡單實例,希望對您有用。如果有疑問,可以聯(lián)系我們。
看完就會,不會你打我,話不多說、開搞(人狠話不多)PHP編程
1.0 首先先看代碼PHP編程
<?php header("Content-Type:text/html;Charset=UTF-8");// 設(shè)置頁面的編碼風格 header("Content-Type:image/jpeg");// 通知瀏覽器輸出的是jpeg格式的圖像 $img = imagecreatetruecolor(150,50);//創(chuàng)建畫布并設(shè)置大小 x軸150 y軸50 $bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景顏色 imagefill($img, 0, 0, $bgcolor); ////把背景填充到圖像 imagejpeg($img); // 輸出圖像 imagedestroy($img); // 銷毀圖像 ?>
好,現(xiàn)在結(jié)合以上代碼,來分析分析以上用到的幾個函數(shù):PHP編程
① imagecreatetruecolor(); PHP編程
imagecreatetruecolor ― 新建一個真彩色圖像(感覺哇,那么長,其實仔細一看挺好記的 image/create/true/color,什么是真彩色圖像?往下看)
PHP編程
resource imagecreatetruecolor ( int $width , int $height )
imagecreatetruecolor() 和?imagecreate()兩個函數(shù)都能創(chuàng)建畫布PHP編程
resource imagecreate ( int $x_size , int $y_size )
imagecreatetruecolor()建立的是一幅大小為 x和 y的黑色圖像(默認為黑色[即便叫法就是真彩色圖像]),如想改變背景顏色則需要用填充顏色函數(shù) imagefill($img,0,0,$color);PHP編程
imagecreate 新建一個空白圖像資源,用imagecolorAllocate()添加背景色PHP編程
上面兩個函數(shù)只不過是一個功能的兩種方法PHP編程
② imagecolorallocate();PHP編程
imagecolorallocate ― 為一幅圖像分配顏色PHP編程
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
顏色分別用 紅 綠 藍三色組合,這些參數(shù)是 0 到 255 的整數(shù)或者十六進制的 0x00 到 0xFF.PHP編程
③ mt_rand();PHP編程
mt_rand ― 生成更好的隨機數(shù)
PHP編程
int mt_rand ( int $min , int $max )
$min 可選的、返回的最小值(默認:0) $max 可選的、返回的最大值(默認:mt_getrandmax())PHP編程
這里就是用來讓他隨機生成背景顏色,0-255隨便取值.所以頁面沒刷新一次畫布背景顏色就不一樣.PHP編程
效果圖:PHP編程
PHP編程
2.0 開始往里面做干擾線,干擾點.防止驗證圖像被秒識別PHP編程
<?php header("Content-Type:text/html;Charset=UTF-8");// 設(shè)置頁面的編碼風格 header("Content-Type:image/jpeg");// 通知瀏覽器輸出的是jpeg格式的圖像 $img = imagecreatetruecolor(150,50);//創(chuàng)建畫布并設(shè)置大小 x軸150 y軸50 $bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景顏色 //添加干擾線,并循環(huán)3次,背景顏色隨機 for($i=0;$i<3;$i++){ $linecolor = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor); } //添加干擾點,并循環(huán)25次,背景顏色隨機 for($i=0;$i<25;$i++){ $dotcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor); } imagefill($img, 0, 0, $bgcolor); ////把背景填充到圖像 imagejpeg($img); // 輸出圖像 imagedestroy($img); // 銷毀圖像 ?>
函數(shù)分析:PHP編程
① imageline();PHP編程
imageline ― 畫一條線段PHP編程
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )PHP編程
imageline() 用 color 顏色在圖像 image 中從坐標 x1,y1 到 x2,y2(圖像左上角為 0, 0)畫一條線段.PHP編程
imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);這里意思就是 畫布$img 中從坐標 x1,y1 到 x2,y2隨機PHP編程
② imagesetpixel();PHP編程
imagesetpixel― 畫一個單一像素PHP編程
bool imagesetpixel ( resource $image , int $x , int $y , int $color )imagesetpixel() 在 image 圖像中用 color 顏色在 x,y 坐標(圖像左上角為 0,0)上畫一個點.PHP編程
imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);具體含義同上 效果圖:
PHP編程
PHP編程
3.0 添加驗證字母數(shù)字PHP編程
<?php header("Content-Type:text/html;Charset=UTF-8");// 設(shè)置頁面的編碼風格 header("Content-Type:image/jpeg");// 通知瀏覽器輸出的是jpeg格式的圖像 $img = imagecreatetruecolor(150,50);//創(chuàng)建畫布并設(shè)置大小 x軸150 y軸50 $bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景顏色 //添加干擾線,并循環(huán)3次,背景顏色隨機 for($i=0;$i<3;$i++){ $linecolor = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor); } //添加干擾點,并循環(huán)25次,背景顏色隨機 for($i=0;$i<25;$i++){ $dotcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor); } //添加需要驗證的字母或者數(shù)字 $rand_str = "qwertyuiopasdfghjklzxcvbnm1234567890";//需要使用到驗證的一些字母和數(shù)字 $str_arr = array(); //命名一個數(shù)組 for($i = 0;$i<4;$i++){ //循環(huán)4次,就是有四個隨機的字母或者數(shù)字 $pos = mt_rand(0,strlen($rand_str)-1); $str_arr[] = $rand_str[$pos];//臨時交換 } $x_start=150/4;//單個字符X軸位置 foreach ($str_arr as $key) { $fontcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); imagettftext($img, 25, mt_rand(-15,15), $x_start, 50/2, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key); $x_start +=20;//遍歷后單個字符沿X軸 +20 } imagefill($img, 0, 0, $bgcolor); ////把背景填充到圖像 imagejpeg($img); // 輸出圖像 imagedestroy($img); // 銷毀圖像 ?>
函數(shù):PHP編程
imagettftext();PHP編程
imagettftext ― 用 TrueType 字體向圖像寫入文本
PHP編程
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
分析下面的代碼:PHP編程
imagettftext($img, 25, mt_rand(-15,15), $x_start, 50/2, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key);PHP編程
$img-----------畫布PHP編程
25-----------字體的尺寸.PHP編程
mt_rand(-15,15)----------角度制表示的角度,0 度為從左向右讀的文本.更高數(shù)值表示逆時針旋轉(zhuǎn).例如 90 度表示從下向上讀的文本.(就是字體角度的問題,)PHP編程
$x_start----------通俗易懂的講就是字符的X軸位置PHP編程
50/2----------字符的高度PHP編程
$fontcolor----------字符顏色PHP編程
"C:/Windows/Fonts/Verdana.TTF"----------字符的字體樣式路徑PHP編程
$key-----------遍歷出后的字符PHP編程
效果:
PHP編程
PHP編程
看起來還是挺可愛的.PHP編程
以上這篇PHP生成制作驗證碼的簡單實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持維易PHP.PHP編程
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/6156.html