PHP生成圖像驗證碼的方法小結(2種方法)__第1頁
PHP生成圖像驗證碼的方法小結(2種方法)__第2頁
PHP生成圖像驗證碼的方法小結(2種方法)__第3頁
PHP生成圖像驗證碼的方法小結(2種方法)__第4頁
PHP生成圖像驗證碼的方法小結(2種方法)__第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、PHP生成圖像驗證碼的方法小結(2種方法)_ 這篇文章主要介紹了PHP生成圖像驗證碼的方法,結合實例形式分析了加法運算驗證碼與字符驗證碼2種方法供大家參考借鑒,需要的伴侶可以參考下 1、生成加法運算驗證碼圖片 session_start (); /*定義頭文件為圖片*/ header(Content-type: image/png); /*生成驗證碼*/ /*創(chuàng)建圖片設置字體顏色*/ $im = imagecreate($w, $h); $red = imagecolorallocate($im, 255, 255, 255); $white = imagecolorallocate($im,

2、 255, 255, 255); /*隨機生成兩個數(shù)字*/ $num1 = rand(1, 20); $num2 = rand(1, 20); $_SESSION administratorConfirmCode = $num1+$num2; /*設置圖片背景顏色*/ $gray = imagecolorallocate($im, 118, 151, 199); $black = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100); /*創(chuàng)建圖片背景*/ imagefilledrectangle($

3、im, 0, 0, 100, 24, $black); /*在畫布上隨機生成大量點*/ for ($i = 0; $i 80; $i+) imagesetpixel($im, rand(0, $w), rand(0, $h), $gray); /*將計算驗證碼寫入到圖片中*/ imagestring($im, 5, 5, 4, $num1, $red); imagestring($im, 5, 30, 3, +, $red); imagestring($im, 5, 45, 4, $num2, $red); imagestring($im, 5, 70, 3, =, $red); images

4、tring($im, 5, 80, 2, ?, $white); /*輸出圖片*/ imagepng($im); imagedestroy($im); 2、生成字符驗證碼圖片【值得留意的是在字體哪里,需要引入實際的字體路徑,否則,可能消失圖像顯示不了驗證碼】 session_start (); /*設置文件頭為圖片輸出*/ Header(Content-type: image/JPEG); /*調用生成驗證碼函數(shù)*/ $str=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234565789; $result=; for($i=0;

5、$i$length;$i+) $num$i=rand(0,61); $result.=$str$num$i; $text = $result; $_SESSION administratorConfirmCode = $text; /*設置圖片的寬度和高度*/ $im_x = $w; $im_y = $y; /*創(chuàng)建圖片*/ $im = imagecreatetruecolor($im_x,$im_y); $text_c = ImageColorAllocate($im, mt_rand(0,100),mt_rand(0,100),mt_rand(0,100); $tmpC0=mt_rand(

6、100,255); $tmpC1=mt_rand(100,255); $tmpC2=mt_rand(100,255); $buttum_c = ImageColorAllocate($im,$tmpC0,$tmpC1,$tmpC2); imagefill($im, 16, 13, $buttum_c); /*字體文件*/ $font = _WEB_DIR_./font/comic.ttf; for ($i=0;$istrlen($text);$i+) $tmp =substr($text,$i,1); $array = array(-1,1); $p = array_rand($array);

7、 $an = $array$p*mt_rand(1,10);/角度 $size = 28; imagettftext($im, $size, $an, 15+$i*$size, 35, $text_c, $font, $tmp); /*將字符寫入文件中*/ $distortion_im = imagecreatetruecolor ($im_x, $im_y); imagefill($distortion_im, 16, 13, $buttum_c); for ( $i=0; $i$im_x; $i+) for ( $j=0; $j$im_y; $j+) $rgb = imagecolorat

8、($im, $i , $j); if( (int)($i+20+sin($j/$im_y*2*M_PI)*10) = imagesx($distortion_im) (int)($i+20+sin($j/$im_y*2*M_PI)*10) =0 ) imagesetpixel ($distortion_im, (int)($i+10+sin($j/$im_y*2*M_PI-M_PI*0.1)*4) , $j , $rgb); /*干擾元素點的數(shù)量*/ $count = 160; /*創(chuàng)建干擾元素點*/ for($i=0; $i$count; $i+) $randcolor = ImageCol

9、orallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255); imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor); /*創(chuàng)建干擾線條*/ $rand = mt_rand(5,30); $rand1 = mt_rand(15,25); $rand2 = mt_rand(5,10); for ($yy=$rand; $yy=+$rand+2; $yy+) for ($px=-80;$px=80;$px=$px+0.1) $x=$px/$rand1; if ($x!=0) $y=sin($x); $py=$y*$rand2; imagesetpixel($distortion_im, $px+80,

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論