產生驗證碼

來源:互聯網
上載者:User
<無詳細內容>
  1. require_once('string.func.php');
  2. function verifyImage( $type=1,$length=4,$pixel=0,$line=0,$sess_name="verify"){
  3. session_start();
  4. /*定義長度和寬度*/
  5. $width=80;
  6. $height=30;
  7. /* 建立畫布*/
  8. $image=imagecreatetruecolor($width, $height);
  9. /*本函數用來匹配圖形的顏色,供其它繪圖函數使用。參數 image 表示圖形的 handle。參數 red、green、blue 是色彩三原色,其值從 0 至 255....我在此定義黑色和白色*/
  10. $white=imagecolorallocate($image, 255, 255, 255);
  11. $black=imagecolorallocate($image,0,0,0);
  12. /*本函數將圖片的封閉長方形地區著色。參數 x1、y1 及 x2、y2 分別為矩形對角線的座標。參數 col 表示欲塗上的顏色*/
  13. imagefilledrectangle($image, 1, 1, $width-2, $height-2, $white);
  14. /*buildRandomString函數用來產生一個驗證碼*/
  15. $chars=buildRandomString($type,$length);
  16. /*將驗證碼給session以便用來判斷使用者輸入是否正確*/
  17. $_SESSION[$sess_name]=$chars;
  18. /*定義字型庫*/
  19. $fontfiles=array('msyh.ttf','msyhbd.ttf','simsun.ttc','SIMYOU.TTF','STHUPO.TTF','STKAITI.TTF','STLITI.TTF');
  20. /*用迴圈來將驗證碼一個一個的寫入圖片中*/
  21. for($i=0;$i<$length;$i++)
  22. {
  23. $size=mt_rand(14,18);
  24. $angle=mt_rand(-15,15);
  25. /*驗證碼的橫座標與縱座標*/
  26. $x=5+$i*$size;
  27. $y=mt_rand(20,26);
  28. $color=imagecolorallocate($image,mt_rand(50,190),mt_rand(50,200),mt_rand(50,90));
  29. $fontfile="../font/".$fontfiles[mt_rand(0,count($fontfiles)-1)];
  30. $text=substr($chars,$i,1);
  31. /*本函數將 TTF (TrueType Fonts) 字型文字寫入圖片*/
  32. imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);
  33. }
  34. if($pixel)
  35. {
  36. for($i=0;$i<50;$i++)
  37. {
  38. /*本函數可在圖片上繪出一點。參數 x、y 為欲繪點的座標,參數 col 表示該點的顏色*/
  39. imagesetpixel($image, mt_rand(0,$width-1), mt_rand(0,$height-1), $black);
  40. }}
  41. if($line)
  42. {
  43. for($i=0;$i<10;$i++)
  44. {
  45. $color=imagecolorallocate($image,mt_rand(50,90),mt_rand(50,200),mt_rand(50,90));
  46. /*畫線段*/
  47. imageline($image, mt_rand(0,$width-1), mt_rand(0,$height-1), mt_rand(0,$width-1), mt_rand(0,$height-1), $color);
  48. }
  49. }
  50. /*以gif形式輸出*/
  51. header("content-type:image/gif");
  52. /*建立GIF圖 並輸出到網頁*/
  53. imagegif($image);
  54. /*釋放與 image 關聯的記憶體*/
  55. imagedestroy($image);
  56. }
複製代碼
  1. function buildRandomString($type=1,$length=4){
  2. if($type==1)
  3. {
  4. /*join函數把數群組轉換為字串。。join() 函數是 implode() 函數的別名*/
  5. $chars=join("",range(0,9));
  6. }elseif ($type==2) {
  7. /*array_merge函數合并數組*/
  8. $chars=join("",array_merge(range("a","z"),range("A","Z")));
  9. }elseif($type==3)
  10. {
  11. $chars=join("",array_merge(range("a","z"),range("A","Z"),range(0,9)));
  12. }
  13. if($length>strlen($chars))
  14. {
  15. exit("字串長度不夠");
  16. }
  17. /*打亂字串*/
  18. $chars=str_shuffle($chars);
  19. return substr($chars,0,$length);
  20. }
  21. ?>
複製代碼
驗證碼
  • 聯繫我們

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