php 驗證碼類 php 驗證碼

來源:互聯網
上載者:User
  1. /**
  2. * 通用驗證碼類 img.php
  3. * 版本:V0.1
  4. * bbs.it-home.org 2013/3/1
  5. */
  6. class ValidateCode {
  7. private $charset="abcdefghizklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; //隨機因子
  8. private $code; //驗證碼文字
  9. private $codelen=4; //驗證碼顯示幾個文字
  10. private $width=130; //驗證碼寬度
  11. private $height=50; //驗證碼高度
  12. private $img; //驗證碼資源控制代碼
  13. private $font; //指定的字型
  14. private $fontsize=20; //指定的字型大小
  15. private $fontcolor; //字型顏色 隨機
  16. //構造類 編寫字型
  17. public function __construct(){
  18. $this->font=ROOT_PATH.'/font/elephant.ttf';
  19. }
  20. //建立4個隨機碼
  21. private function createCode(){
  22. $_leng=strlen($this->charset);
  23. for($i=1;$i<=$this->codelen;$i++){
  24. $this->code.=$this->charset[mt_rand(0,$_leng)];
  25. }
  26. return $this->code;
  27. }
  28. //建立背景
  29. private function createBg(){
  30. //建立畫布 給一個資源jubing
  31. $this->img=imagecreatetruecolor($this->width,$this->height);
  32. //背景顏色
  33. $color=imagecolorallocate($this->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));
  34. //畫出一個矩形
  35. imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
  36. }
  37. //建立字型
  38. private function createFont(){
  39. $_x=($this->width / $this->codelen); //字型長度
  40. for ($i=0;$i<$this->codelen;$i++){
  41. //文字顏色
  42. $color=imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
  43. //資源控制代碼 字型大小 傾斜度 字型長度 字型高度 字型顏色 字型 具體文本
  44. imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height/1.4,$color,$this->font,$this->code[$i]);
  45. }
  46. }
  47. //隨機線條
  48. private function createLine(){
  49. //隨機線條
  50. for ($i=0;$i<6;$i++){
  51. $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
  52. imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
  53. }
  54. //隨機雪花
  55. for ($i=0;$i<45;$i++){
  56. $color = imagecolorallocate($this->img,mt_rand(220,255),mt_rand(220,255),mt_rand(220,255));
  57. imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
  58. }
  59. }
  60. //輸出背景
  61. private function outPut(){
  62. //產生標題
  63. header('ContentType:img/png');
  64. //輸出圖片
  65. imagepng($this->img);
  66. //銷毀結果集
  67. imagedestroy($this->img);
  68. }
  69. //對外輸出
  70. public function doimg(){
  71. //載入背景
  72. $this->createBg();
  73. //負載檔案
  74. $this->createCode();
  75. //載入線條
  76. $this->createLine();
  77. //載入字型
  78. $this->createFont();
  79. //載入背景
  80. $this->outPut();
  81. }
  82. //擷取驗證碼
  83. public function getCode(){
  84. return strtolower($this->code);
  85. }
  86. }
  87. ?>
複製代碼

調用樣本:index.php 為大家推薦幾篇有關php驗證碼的文章:php隨機驗證碼 php產生隨機驗證碼(圖文)用php產生帶有雪花背景的驗證碼php寫的一個驗證碼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.