《PHP實例:最新最全PHP生成制作驗證碼代碼詳解(推薦)》要點:
本文介紹了PHP實例:最新最全PHP生成制作驗證碼代碼詳解(推薦),希望對您有用。如果有疑問,可以聯系我們。
PHP實例1.0 首先先看代碼
PHP實例
<?php
header("Content-Type:text/html;Charset=UTF-");// 設置頁面的編碼風格
header("Content-Type:image/jpeg");// 通知瀏覽器輸出的是jpeg格式的圖像
$img = imagecreatetruecolor(,);//創建畫布并設置大小 x軸 y軸
$bgcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));//分配背景顏色
imagefill($img, , , $bgcolor); ////把背景填充到圖像
imagejpeg($img); // 輸出圖像
imagedestroy($img); // 銷毀圖像
?>
PHP實例好,現在結合以上代碼,來分析分析以上用到的幾個函數:
PHP實例① imagecreatetruecolor();
PHP實例imagecreatetruecolor ― 新建一個真彩色圖像(感覺哇,那么長,其實仔細一看挺好記的 image/create/true/color,什么是真彩色圖像?往下看)
PHP實例
resource imagecreatetruecolor ( int $width , int $height )
PHP實例imagecreatetruecolor() 和 imagecreate()兩個函數都能創建畫布
PHP實例
resource imagecreate ( int $x_size , int $y_size )
PHP實例imagecreatetruecolor()建立的是一幅大小為 x和 y的黑色圖像(默認為黑色[即便叫法就是真彩色圖像]),如想改變背景顏色則需
PHP實例要用填充顏色函數 imagefill($img,0,0,$color);
PHP實例imagecreate 新建一個空白圖像資源,用imagecolorAllocate()添加背景色
PHP實例上面兩個函數只不過是一個功能的兩種方法
PHP實例② imagecolorallocate();
PHP實例imagecolorallocate ― 為一幅圖像分配顏色
PHP實例
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
PHP實例顏色分別用 紅 綠 藍三色組合,這些參數是 0 到 255 的整數或者十六進制的 0x00 到 0xFF.
PHP實例③ mt_rand();
PHP實例mt_rand ― 生成更好的隨機數
PHP實例
int mt_rand ( int $min , int $max )
PHP實例$min 可選的、返回的最小值(默認:0) $max 可選的、返回的最大值(默認:mt_getrandmax())
這里就是用來讓他隨機生成背景顏色,0-255隨便取值.所以頁面沒刷新一次畫布背景顏色就不一樣.效果圖:
PHP實例
PHP實例2.0 開始往里面做干擾線,干擾點.防止驗證圖像被秒識別
PHP實例
<?php
header("Content-Type:text/html;Charset=UTF-");// 設置頁面的編碼風格
header("Content-Type:image/jpeg");// 通知瀏覽器輸出的是jpeg格式的圖像
$img = imagecreatetruecolor(,);//創建畫布并設置大小 x軸 y軸
$bgcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));//分配背景顏色
//添加干擾線,并循環次,背景顏色隨機
for($i=;$i<;$i++){
$linecolor = imagecolorallocate($img,mt_rand(,),mt_rand(,),mt_rand(,));
imageline($img, mt_rand(,), mt_rand(,), mt_rand(,), mt_rand(,), $linecolor);
}
//添加干擾點,并循環次,背景顏色隨機
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); // 銷毀圖像
?>
PHP實例函數分析:
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 )
PHP實例imagesetpixel() 在 image 圖像中用 color 顏色在 x,y 坐標(圖像左上角為 0,0)上畫一個點.
PHP實例
imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);具體含義同上
PHP實例效果圖:
PHP實例
PHP實例3.0 添加驗證字母數字
PHP實例
<?php
header("Content-Type:text/html;Charset=UTF-");// 設置頁面的編碼風格
header("Content-Type:image/jpeg");// 通知瀏覽器輸出的是jpeg格式的圖像
$img = imagecreatetruecolor(,);//創建畫布并設置大小 x軸 y軸
$bgcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));//分配背景顏色
//添加干擾線,并循環次,背景顏色隨機
for($i=;$i<;$i++){
$linecolor = imagecolorallocate($img,mt_rand(,),mt_rand(,),mt_rand(,));
imageline($img, mt_rand(,), mt_rand(,), mt_rand(,), mt_rand(,), $linecolor);
}
//添加干擾點,并循環次,背景顏色隨機
for($i=;$i<;$i++){
$dotcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));
imagesetpixel($img, mt_rand(,), mt_rand(,), $dotcolor);
}
//添加需要驗證的字母或者數字
$rand_str = "qwertyuiopasdfghjklzxcvbnm";//需要使用到驗證的一些字母和數字
$str_arr = array(); //命名一個數組
for($i = ;$i<;$i++){ //循環次,就是有四個隨機的字母或者數字
$pos = mt_rand(,strlen($rand_str)-);
$str_arr[] = $rand_str[$pos];//臨時交換
}
$x_start=/;//單個字符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 +=;//遍歷后單個字符沿X軸 +
}
imagefill($img, , , $bgcolor); ////把背景填充到圖像
imagejpeg($img); // 輸出圖像
imagedestroy($img); // 銷毀圖像
?>
PHP實例函數:
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實例分析下面的代碼:
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 度為從左向右讀的文本.更高數值表示逆時針旋轉.例如 90 度表示從下向上讀的文本.(就是字體角度的問題,)
PHP實例$x_start----------通俗易懂的講就是字符的X軸位置
PHP實例50/2----------字符的高度
PHP實例$fontcolor----------字符顏色
PHP實例"C:/Windows/Fonts/Verdana.TTF"----------字符的字體樣式路徑
PHP實例$key-----------遍歷出后的字符
PHP實例效果:
PHP實例
PHP實例以上內容是本文給大家介紹的最新最全PHP生成制作驗證碼代碼詳解(推薦)的全部敘述,希望對大家有所幫助!
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/6248.html