- $authnum='';
- $ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
- $list=explode(",",$ychar);//分割函數
- for($i=0;$i<4;$i++){
- $randnum=rand(0,35);
- $authnum.=$list[$randnum];//以數組的形式輸出
複製代碼方法二,定義為一個私人函數。
- private function createCheckCode()
- {
- for(i=0;icodeNum;i++)
- {
- number = rand(0,2);
- switch(number)
- {
- case 0: rand_number = rand(48,57); break;//數字
- case 1: rand_number = rand(65,90);break;//大寫字母
- case 2: rand_number = rand(97,122);break;//小寫字母
- }
- $asc = sprintf("%c",rand_number);
- $asc_number = asc_number.asc;
- }
- return asc_number;
- }
複製代碼方法三,使用隨機種子產生php驗證碼。
- srand(microtime()*100000);//相當於計時器
- $string="abcdefghigklmnopqrstuvwxyz123456789";
- for($i=0;$i<4;$i++)
- {
- $new_number.=$string[rand(0,strlen($string)-1)];//隨即的產生一個數組
- }
複製代碼方法四,
- for($i=0;$i<4;$i++)
- {
- $rand.=dechex(rand(1,15));//將十進位轉化為十六進位
- }
複製代碼接下來進入本文的重點:PHP GD庫:(提供了一系列圖片處理函數的IPI,產生圖片處理圖片) 啟用php中GD庫:php.ini設定檔中,去掉";extension=php_gd2.dll"中“;”; 部分GD庫函數的介紹:1.imagecreatetruecolor(int x_size,int Y_size) 建立真彩色映像 2.imagecolorallocate(resource image,int red,int green,int blue) 為一幅映像分配顏色,三原色 3.imagestring(resource,font,int x,int y,content,color)繪圖函數4.header("Content-type:image/jpeg") 輸出函數php的header是定義頭的動作,php5中支援3中類型: 1,Content-type:xxxx/yyyy 2,Location:xxxx:yyyy/zzzz 3,Status:nnn xxxxxx xxxx/yyyy表示內容檔案的類型 如:image/gif image/jpeg image/png imagejpeg(),imagegif(),imagepang() 5.iamgeline(resource image,int x1,int y1,int x2,int y2,int color); 畫線函數,(int x,int y)起始座標6.imagesetpixel(resource image,int x,int y,int color) 畫點函數7.imagettftext(resource image,float size,float angle,int x,int y,int color,string fontfile,string text) 帶字型寫入函數8.iconv("gb2312","utf-8","字串");//首先要將文字轉換成utf-8格式 php驗證碼插入中文的方法,這裡可以參考下php 中文驗證碼的產生方法。 例1,隨機產生數字,字母的代碼:
- //che.php
- session_start();
- for($i=0;$i<4;$i++)
- {
- $rand.=dechex(rand(1,15));
- }
- $_SESSION['check_num']=$rand;
- $image=imagecreatetruecolor(50,30);
- $bg=imagecolorallocate($im,0,0,0);//第一次用調色盤的時候,背景顏色
- $te=imagecolorallocate($im,255,255,255);
- imagestring($image,6,rand(0,20),rand(0,2),$rand,$te);
- ob_clean();//PHP網頁中因為 要產生驗證碼而出現 映像"http://localhost/**.php"因其本身有錯無法顯示
- header("Content-type:image/jpeg"); imagejpeg($image);
- ?>
複製代碼例2,給圖片畫出幹擾線代碼:
- for($i=0;$i<8;$i++)//畫出多條線
- {
- $cg=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//產生隨機的顏色
- imageline($im,rand(10,40),0,rand(10,40),20,$cg);
- }
複製代碼例3,給圖片畫出幹擾點的代碼:
- for($i=0;$i<80;$i++)//畫出多個點
- {
- imagesetpixel($im,rand(0,40),rand(0,20),$cg);
- }
複製代碼例4,把文字寫入圖片代碼:
$str=array('我','我','親','親');//儲存顯示的漢字
- for($i=0;$i<4;$i++)
- {
- $sss.=$str[rand(0,3)];//隨機顯示漢字
- }
//$str=iconv("gb2312","utf-8",$str); //漢字編碼轉化,我的好像不需要
- imagettftext($im,10,0,rand(5,60),rand(5,60),$te,"simhei.ttf",$sss);//
複製代碼0:字型的傾斜度,“simhei.ttf”:字型樣式,一般放在根目錄下; 如此便完成了php 圖片驗證碼的整個製作過程,指令碼小編希望本文對使用gd庫產生php驗證碼,有一定的協助。 |