最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

最新最全PHP生成制作驗證碼代碼詳解(推薦)

 更新時間:2016年06月12日 09:05:54   作者:秋田嘉  
這篇文章主要介紹了最新最全PHP生成制作驗證碼代碼詳解(推薦) 的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下

1.0  首先先看代碼

<?php
header("Content-Type:text/html;Charset=UTF-");// 設(shè)置頁面的編碼風(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é)合以上代碼,來分析分析以上用到的幾個函數(shù):

①  imagecreatetruecolor();

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

resource imagecreatetruecolor ( int $width , int $height )

imagecreatetruecolor() 和 imagecreate()兩個函數(shù)都能創(chuàng)建畫布

resource imagecreate ( int $x_size , int $y_size ) 

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

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

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

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

②  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 — 生成更好的隨機數(shù)

int mt_rand ( int $min , int $max )

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

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

<?php
header("Content-Type:text/html;Charset=UTF-");// 設(shè)置頁面的編碼風(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)次,背景顏色隨機
for($i=;$i<;$i++){
$linecolor = imagecolorallocate($img,mt_rand(,),mt_rand(,),mt_rand(,));
imageline($img, mt_rand(,), mt_rand(,), mt_rand(,), mt_rand(,), $linecolor);
}
//添加干擾點,并循環(huán)次,背景顏色隨機
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隨機

②  imagesetpixel();

imagesetpixel— 畫一個單一像素

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

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

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

效果圖:


3.0  添加驗證字母數(shù)字

<?php
header("Content-Type:text/html;Charset=UTF-");// 設(shè)置頁面的編碼風(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)次,背景顏色隨機
for($i=;$i<;$i++){
$linecolor = imagecolorallocate($img,mt_rand(,),mt_rand(,),mt_rand(,));
imageline($img, mt_rand(,), mt_rand(,), mt_rand(,), mt_rand(,), $linecolor);
}
//添加干擾點,并循環(huán)次,背景顏色隨機
for($i=;$i<;$i++){
$dotcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));
imagesetpixel($img, mt_rand(,), mt_rand(,), $dotcolor);
}
//添加需要驗證的字母或者數(shù)字
$rand_str = "qwertyuiopasdfghjklzxcvbnm";//需要使用到驗證的一些字母和數(shù)字
$str_arr = array(); //命名一個數(shù)組
for($i = ;$i<;$i++){ //循環(huán)次,就是有四個隨機的字母或者數(shù)字 
$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); // 銷毀圖像
?> 

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

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

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

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

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

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

效果:


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

相關(guān)文章

  • smarty簡單入門實例

    smarty簡單入門實例

    這篇文章主要介紹了smarty簡單入門實例,包括了配置文件的用法與模板文件的使用,非常具有實用價值,需要的朋友可以參考下
    2014-11-11
  • 如何通過Linux命令行使用和運行PHP腳本

    如何通過Linux命令行使用和運行PHP腳本

    這篇文章主要介紹了如何通過Linux命令行使用和運行PHP腳本,PHP語言和C/Java以及嗲有一些PHP特性的Perl變成語言中的語法非常相似,當(dāng)前比較穩(wěn)定且最新的版本是5.6.10。PHP是一種HTML的嵌入腳本,很方便開發(fā)人員寫出動態(tài)生成的頁面,需要的朋友可以參考下
    2015-07-07
  • php操作mysql數(shù)據(jù)庫的基本類代碼

    php操作mysql數(shù)據(jù)庫的基本類代碼

    這篇文章主要介紹了php操作mysql數(shù)據(jù)庫的基本類代碼,需要的朋友可以參考下
    2014-02-02
  • ThinkPHP2.0讀取MSSQL提示Incorrect syntax near the keyword ''AS''的解決方法

    ThinkPHP2.0讀取MSSQL提示Incorrect syntax near the keyword ''AS''

    這篇文章主要介紹了ThinkPHP2.0讀取MSSQL提示Incorrect syntax near the keyword 'AS'的解決方法,需要的朋友可以參考下
    2014-06-06
  • ThinkPHP3.1新特性之多層MVC的支持

    ThinkPHP3.1新特性之多層MVC的支持

    默認(rèn)的模型層由Model類構(gòu)成,但是隨著項目的增大和業(yè)務(wù)體系的復(fù)雜化,單一的模型層很難解決要求,從3.1開始推出了多層Model的支持。這篇文章主要介紹了ThinkPHP3.1多層MVC的支持,需要的朋友可以參考下
    2014-06-06
  • PHP對接微信公眾平臺消息接口開發(fā)流程教程

    PHP對接微信公眾平臺消息接口開發(fā)流程教程

    這篇文章主要介紹了PHP對接微信公眾平臺消息接口開發(fā)流程,如何使用PHP版接口操作公眾平臺消息,需要的朋友可以參考下
    2014-03-03
  • 基于php+mysql的期末作業(yè)小項目(學(xué)生信息管理系統(tǒng))

    基于php+mysql的期末作業(yè)小項目(學(xué)生信息管理系統(tǒng))

    最近自己寫的一個簡單的php期末作業(yè)項目,分享給大家,下面這篇文章主要給大家介紹了關(guān)于基于php+mysql的期末作業(yè)小項目,一個學(xué)生信息管理系統(tǒng),需要的朋友可以參考下
    2023-01-01
  • PHP實現(xiàn)八皇后算法

    PHP實現(xiàn)八皇后算法

    八皇后問題,是一個古老而著名的問題,是回溯算法的典型案例。這篇文章主要介紹了PHP實現(xiàn)八皇后算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • php的ajax簡單實例

    php的ajax簡單實例

    本篇文章主要是對php的ajax簡單實例進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-02-02
  • php實現(xiàn)評論回復(fù)刪除功能

    php實現(xiàn)評論回復(fù)刪除功能

    這篇文章主要為大家詳細(xì)介紹了php實現(xiàn)評論回復(fù)刪除功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05

最新評論

孙吴县| 乌兰察布市| 镇巴县| 广河县| 万全县| 潢川县| 太康县| 威海市| 米泉市| 宜丰县| 泽普县| 湛江市| 西宁市| 武胜县| 永善县| 吉木乃县| 开阳县| 宁陕县| 临泽县| 伊吾县| 临漳县| 马公市| 涪陵区| 重庆市| 化州市| 新巴尔虎右旗| 孝义市| 黔江区| 东辽县| 海口市| 同江市| 莎车县| 汤阴县| 诏安县| 中卫市| 卢湾区| 通河县| 明溪县| 尼玛县| 文山县| 富裕县|