PHP產生製作驗證碼,php產生驗證碼_PHP教程

來源:互聯網
上載者:User

PHP產生製作驗證碼,php產生驗證碼


看完就會,不會你打我,話不多說、開搞(人狠話不多)

1.0  首先先看代碼

 1 php 2 header("Content-Type:text/html;Charset=UTF-8");// 設定頁面的編碼風格 3 header("Content-Type:image/jpeg");// 通知瀏覽器輸出的是jpeg格式的映像 4  5 $img = imagecreatetruecolor(150,50);//建立畫布並設定大小  x軸150  y軸50 6  7 $bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景顏色 8 imagefill($img, 0, 0, $bgcolor); ////把背景填充到映像 9 imagejpeg($img);             // 輸出映像10 imagedestroy($img);          // 銷毀映像11 ?>

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

①  imagecreatetruecolor();

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

1 resource imagecreatetruecolor ( int $width , int $height )

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

1 resource imagecreate ( int $x_size , int $y_size )

imagecreatetruecolor()建立的是一幅大小為 x和 y的黑色映像(預設為黑色[即便叫法就是真彩色映像]),如想改變背景顏色則需要用填充顏色函數 imagefill($img,0,0,$color);

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

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

②  imagecolorallocate();

imagecolorallocate — 為一幅映像分配顏色

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

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

③  mt_rand();

mt_rand — 產生更好的隨機數

1 int mt_rand ( int $min , int $max )

$min 可選的、返回的最小值(預設:0)  $max 可選的、返回的最大值(預設:mt_getrandmax())

這裡就是用來讓他隨機產生背景顏色,0-255隨便取值。所以頁面沒重新整理一次畫布背景顏色就不一樣。

2.0  開始往裡面做幹擾線,幹擾點。防止驗證映像被秒識別

 1 php 2 header("Content-Type:text/html;Charset=UTF-8");// 設定頁面的編碼風格 3 header("Content-Type:image/jpeg");// 通知瀏覽器輸出的是jpeg格式的映像 4  5 $img = imagecreatetruecolor(150,50);//建立畫布並設定大小  x軸150  y軸50 6  7 $bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景顏色 8  9 //添加幹擾線,並迴圈3次,背景顏色隨機10 for($i=0;$i<3;$i++){11 12     $linecolor = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));13     imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);14 15 }16 //添加幹擾點,並迴圈25次,背景顏色隨機17 for($i=0;$i<25;$i++){18 19     $dotcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));20     imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);21 22 }23 24 imagefill($img, 0, 0, $bgcolor); ////把背景填充到映像25 imagejpeg($img);             // 輸出映像26 imagedestroy($img);          // 銷毀映像27 ?>

函數分析:

①  imageline();

imageline — 畫一條線段

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

imageline() 用 color 顏色在映像 image 中從座標 x1y1x2y2(映像左上方為 0, 0)畫一條線段。

imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);

這裡意思就是 畫布$img 中從座標 x1y1x2y2隨機


②  imagesetpixel();

imagesetpixel— 畫一個單一像素

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

imagesetpixel() 在 image 映像中用 color 顏色在 xy 座標(映像左上方為 0,0)上畫一個點。

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

3.0  添加驗證字母數字

 1 php 2 header("Content-Type:text/html;Charset=UTF-8");// 設定頁面的編碼風格 3 header("Content-Type:image/jpeg");// 通知瀏覽器輸出的是jpeg格式的映像 4  5 $img = imagecreatetruecolor(150,50);//建立畫布並設定大小  x軸150  y軸50 6  7 $bgcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));//分配背景顏色 8  9 //添加幹擾線,並迴圈3次,背景顏色隨機10 for($i=0;$i<3;$i++){11 12     $linecolor = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));13     imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);14 15 }16 //添加幹擾點,並迴圈25次,背景顏色隨機17 for($i=0;$i<25;$i++){18 19     $dotcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));20     imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);21 22 }23 24 //添加需要驗證的字母或者數字25 $rand_str = "qwertyuiopasdfghjklzxcvbnm1234567890";//需要使用到驗證的一些字母和數字26 $str_arr = array();    //命名一個數組27 for($i = 0;$i<4;$i++){    //迴圈4次,就是有四個隨機的字母或者數字                            28     $pos = mt_rand(0,strlen($rand_str)-1);29     $str_arr[] = $rand_str[$pos];//臨時交換30 }31 32 $x_start=150/4;//單個字元X軸位置33 34 foreach ($str_arr as $key) {35     $fontcolor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));36     imagettftext($img, 25, mt_rand(-15,15), $x_start, 50/2, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key);37     $x_start +=20;//遍曆後單個字元沿X軸 +2038 }39 40 imagefill($img, 0, 0, $bgcolor); ////把背景填充到映像41 imagejpeg($img);             // 輸出映像42 imagedestroy($img);          // 銷毀映像43 ?>

函數:

imagettftext();

imagettftext — 用 TrueType 字型向映像寫入文本

1 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-----------遍曆出後的字元

效果:

看起來還是挺可愛的。

http://www.bkjia.com/PHPjc/1133417.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133417.htmlTechArticlePHP產生製作驗證碼,php產生驗證碼 看完就會,不會你打我,話不多說、開搞( 人狠話不多 ) 1.0 首先先看代碼 1 ? php 2 header ("Content-Type:t...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.