成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? 最新最全PHP生成制作驗證碼代碼詳解(推薦)
全站頭部文字 我要出現在這里
最新最全PHP生成制作驗證碼代碼詳解(推薦) 752 2024-01-31   

1.0  首先先看代碼

<?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); // 銷毀圖像
?> 

好,現在結合以上代碼,來分析分析以上用到的幾個函數:

①  imagecreatetruecolor();

imagecreatetruecolor — 新建一個真彩色圖像(感覺哇,那么長,其實仔細一看挺好記的 image/create/true/color,什么是真彩色圖像?往下看)

resource imagecreatetruecolor ( int $width , int $height )

imagecreatetruecolor() 和 imagecreate()兩個函數都能創建畫布

resource imagecreate ( int $x_size , int $y_size ) 

imagecreatetruecolor()建立的是一幅大小為 x和 y的黑色圖像(默認為黑色[即便叫法就是真彩色圖像]),如想改變背景顏色則需

要用填充顏色函數 imagefill($img,0,0,$color);

imagecreate 新建一個空白圖像資源,用imagecolorAllocate()添加背景色

上面兩個函數只不過是一個功能的兩種方法

②  imagecolorallocate();

imagecolorallocate — 為一幅圖像分配顏色

int imagecolorallocate ( resource $image , int $red , int $green , int $blue )

顏色分別用 紅 綠 藍三色組合,這些參數是 0 到 255 的整數或者十六進制的 0x00 到 0xFF。

③  mt_rand();

mt_rand — 生成更好的隨機數

int mt_rand ( int $min , int $max )

$min 可選的、返回的最小值(默認:0)  $max 可選的、返回的最大值(默認:mt_getrandmax())
這里就是用來讓他隨機生成背景顏色,0-255隨便取值。所以頁面沒刷新一次畫布背景顏色就不一樣。效果圖:

查看圖片

2.0  開始往里面做干擾線,干擾點。防止驗證圖像被秒識別

<?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); // 銷毀圖像
?> 

函數分析:

①  imageline();

imageline — 畫一條線段

bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color ) 

imageline() 用 color 顏色在圖像 image 中從坐標 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 中從坐標 x1,y1 到 x2,y2隨機

②  imagesetpixel();

imagesetpixel— 畫一個單一像素

bool imagesetpixel ( resource $image , int $x , int $y , int $color )

imagesetpixel() 在 image 圖像中用 color 顏色在 x,y 坐標(圖像左上角為 0,0)上畫一個點。

imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);具體含義同上 

效果圖:

查看圖片

3.0  添加驗證字母數字

<?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); // 銷毀圖像
?> 

函數:

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 度為從左向右讀的文本。更高數值表示逆時針旋轉。例如 90 度表示從下向上讀的文本。(就是字體角度的問題,)

$x_start----------通俗易懂的講就是字符的X軸位置

50/2----------字符的高度

$fontcolor----------字符顏色

"C:/Windows/Fonts/Verdana.TTF"----------字符的字體樣式路徑

$key-----------遍歷出后的字符

效果:

查看圖片

以上內容是本文給大家介紹的最新最全PHP生成制作驗證碼代碼詳解(推薦)的全部敘述,希望對大家有所幫助!


PHP

主站蜘蛛池模板: 焦波| 二手大棚钢管急卖2000元| 漫画头像女生可爱| 天天快乐视频免费观看下载| 美妙旋律| 数码宝贝第三部| 春ppt| 风筝 电影| 金政| 《雪中悍刀行》主演| 新红楼梦惊艳版| 金发女郎| 俩组词拼音| 丹尼尔·吉里斯| 生猴子视频| 父子激情视频| 放下一个人最狠的网名| 寡妇2| 青山知可子冲绳草莽英雄1969| 丛林之王| 抖音登录网页版| 乱世危情电视剧演员表| 《西游降魔篇》2| 女同激情视频| 老公看你的| 雪中悍刀行第一季电视剧免费观看| 淡蓝色的雨简谱| 敖丙手机壁纸| 国产精品久久久久精品三级ⅰ| 母亲电影韩国完整版免费观看| 拜金女郎| 雨后的故事34张原版视频| 女人战争之肮脏的交易| 大学英语综合教程1电子书| 电影《上一当》| 我的吸血鬼学姐| 焕羽电视剧免费播放在线观看| 黄视频免费在线| 下女们| 高手论坛| 大团圆李静张娴|

!!!站長長期在線接!!!

網站、小程序:定制開發/二次開發/仿制開發等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103