PHP圖片驗證碼的製作執行個體

來源:互聯網
上載者:User
  1. $authnum='';
  2. $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";
  3. $list=explode(",",$ychar);//分割函數
  4. for($i=0;$i<4;$i++){
  5. $randnum=rand(0,35);
  6. $authnum.=$list[$randnum];//以數組的形式輸出
複製代碼

方法二,定義為一個私人函數。

  1. private function createCheckCode()
  2. {
  3. for(i=0;icodeNum;i++)
  4. {
  5. number = rand(0,2);
  6. switch(number)
  7. {
  8. case 0: rand_number = rand(48,57); break;//數字
  9. case 1: rand_number = rand(65,90);break;//大寫字母
  10. case 2: rand_number = rand(97,122);break;//小寫字母
  11. }
  12. $asc = sprintf("%c",rand_number);
  13. $asc_number = asc_number.asc;
  14. }
  15. return asc_number;
  16. }
複製代碼

方法三,使用隨機種子產生php驗證碼。

  1. srand(microtime()*100000);//相當於計時器
  2. $string="abcdefghigklmnopqrstuvwxyz123456789";
  3. for($i=0;$i<4;$i++)
  4. {
  5. $new_number.=$string[rand(0,strlen($string)-1)];//隨即的產生一個數組
  6. }
複製代碼

方法四,

  1. for($i=0;$i<4;$i++)
  2. {
  3. $rand.=dechex(rand(1,15));//將十進位轉化為十六進位
  4. }
複製代碼

接下來進入本文的重點: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,隨機產生數字,字母的代碼:

  1. //che.php
  2. session_start();
  3. for($i=0;$i<4;$i++)
  4. {
  5. $rand.=dechex(rand(1,15));
  6. }
  7. $_SESSION['check_num']=$rand;
  8. $image=imagecreatetruecolor(50,30);
  9. $bg=imagecolorallocate($im,0,0,0);//第一次用調色盤的時候,背景顏色
  10. $te=imagecolorallocate($im,255,255,255);
  11. imagestring($image,6,rand(0,20),rand(0,2),$rand,$te);
  12. ob_clean();//PHP網頁中因為 要產生驗證碼而出現 映像"http://localhost/**.php"因其本身有錯無法顯示
  13. header("Content-type:image/jpeg"); imagejpeg($image);
  14. ?>
複製代碼

例2,給圖片畫出幹擾線代碼:

  1. for($i=0;$i<8;$i++)//畫出多條線
  2. {
  3. $cg=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//產生隨機的顏色
  4. imageline($im,rand(10,40),0,rand(10,40),20,$cg);
  5. }
複製代碼

例3,給圖片畫出幹擾點的代碼:

  1. for($i=0;$i<80;$i++)//畫出多個點
  2. {
  3. imagesetpixel($im,rand(0,40),rand(0,20),$cg);
  4. }
複製代碼

例4,把文字寫入圖片代碼:

  1. $str=array('我','我','親','親');//儲存顯示的漢字

  2. for($i=0;$i<4;$i++)
  3. {
  4. $sss.=$str[rand(0,3)];//隨機顯示漢字
  5. }

  6. //$str=iconv("gb2312","utf-8",$str); //漢字編碼轉化,我的好像不需要

  7. imagettftext($im,10,0,rand(5,60),rand(5,60),$te,"simhei.ttf",$sss);//

複製代碼

0:字型的傾斜度,“simhei.ttf”:字型樣式,一般放在根目錄下;

如此便完成了php 圖片驗證碼的整個製作過程,指令碼小編希望本文對使用gd庫產生php驗證碼,有一定的協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.