国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP驗(yàn)證碼生成類完整代碼

2024-09-04 11:44:30
字體:
供稿:網(wǎng)友

本文章提供這款php驗(yàn)證碼生成類靈活好用,用戶可以定義各個(gè)成員 有寬、高、畫布、字?jǐn)?shù)、類型、畫類型同時(shí)我們只要修改 $Type就可以定義生成的是純數(shù)字,純小寫字母,大小寫數(shù)字混合,有需要的朋友可參考.

PHP驗(yàn)證碼生成類完整代碼如下:

  1. <?php 
  2. class Code{ 
  3.  
  4. // 1. 定義各個(gè)成員 有寬、高、畫布、字?jǐn)?shù)、類型、畫類型 
  5.  
  6. private $width//寬度 
  7. private $height//高度 
  8. private $num//驗(yàn)證碼字?jǐn)?shù) 
  9. private $imgType//生成圖片類型 
  10. private $Type//字串類型 1,2,3 三個(gè)選項(xiàng) 1 純數(shù)字 2 純小寫字母 3 大小寫數(shù)字混合 
  11. private $hb//畫布 
  12. public $codestr// 驗(yàn)證碼字串 
  13.  
  14. public function __construct($height=20,$num=4,$imgType="jpeg",$Type=1){ 
  15. $this->width = $num*20; 
  16. $this->height = $height
  17. $this->num = $num
  18. $this->imgType = $imgType;  
  19. $this->Type = $Type;  
  20. $this->codestr = $this->codestr(); 
  21. $this->zuhe(); 
  22.  
  23. // 2. 定義隨機(jī)獲取字符串函數(shù) 
  24. private function codestr(){ 
  25. switch($this->Type){ 
  26.  
  27. case 1: // 類型為1 獲取1-9隨機(jī)數(shù) 
  28. $str = implode("",array_rand(range(0,9),$this->num)); 
  29. break
  30. case 2: // 類型為2 獲取a-z隨機(jī)小寫字母 
  31. $str = implode("",array_rand(array_flip(range(a,z)),$this->num)); 
  32. break
  33. case 3: // 類型為3 獲取數(shù)字,小寫字母,大寫字母 混合 
  34. for($i=0;$i<$this->num;$i++){ 
  35. $m = rand(0,2); 
  36. switch($m){ 
  37. case 0: 
  38. $o = rand(48,57); 
  39. break
  40. case 1: 
  41. $o = rand(65,90); 
  42. break
  43. case 2: 
  44. $o = rand(97,122); 
  45. break;  
  46. $str .= sprintf("%c",$o); 
  47. break;  
  48.  
  49.  
  50. return $str;  
  51.  
  52.  
  53. // 3. 初始化畫布圖像資源 
  54. private function Hb(){ 
  55. $this->hb = imagecreatetruecolor($this->width,$this->height);  
  56.  
  57. // 4. 生成背景顏色 
  58. private function Bg(){ 
  59. return imagecolorallocate($this->hb,rand(130,250),rand(130,250),rand(130,250));  
  60.  
  61. // 5. 生成字體顏色 
  62. private function Font(){ 
  63. return imagecolorallocate($this->hb,rand(0,100),rand(0,100),rand(0,100));  
  64.  
  65. // 6. 填充背景顏色 
  66. private function BgColor(){ 
  67. imagefilledrectangle($this->hb,0,0,$this->width,$this->height,$this->Bg());  
  68.  
  69. // 7. 干擾點(diǎn) 
  70. private function ganrao(){ 
  71. $sum=floor(($this->width)*($this->height)/3); 
  72. for($i=0;$i<$sum;$i++){ 
  73. imagesetpixel($this->hb,rand(0,$this->width),rand(0,$this->height),$this->Bg());  
  74.  
  75. // 8. 隨機(jī)直線 弧線 
  76. private function huxian(){ 
  77. for($i=0;$i<$this->num;$i++){ 
  78. imageArc($this->hb,rand(0,$this->width),rand(0,$this->height),rand(0,$this->width),rand(0,$this->height),rand(0,360),rand(0,360),$this->Bg());  
  79. }  
  80.  
  81. // 9. 寫字 
  82. private function xiezi(){ 
  83. for($i=0;$i<$this->num;$i++){ 
  84. $x=ceil($this->width/$this->num)*$i;  
  85. $y=rand(1,$this->height-15); 
  86. imagechar($this->hb,5,$x+4,$y,$this->codestr[$i],$this->Font()); 
  87. }  
  88.  
  89. // 10. 輸出 
  90. private function OutImg(){ 
  91. $shuchu="image".$this->imgType;  
  92. $header="Content-type:image/".$this->imgType; 
  93. if(function_exists($shuchu)){ 
  94. header($header); 
  95. $shuchu($this->hb);  
  96. }else
  97. exit("GD庫沒有此類圖像");  
  98.  
  99. // 11. 拼裝 
  100. private function zuhe(){ 
  101. $this->Hb(); 
  102. $this->BgColor(); 
  103. $this->ganrao(); 
  104. $this->huxian(); 
  105. $this->xiezi(); 
  106. $this->OutImg();  
  107. }//開源代碼Vevb.com 
  108.  
  109. public function getCodeStr(){ 
  110. return $this->codestr;  
  111. ?> 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 金塔县| 盘山县| 寻甸| 清水河县| 扎鲁特旗| 雷波县| 蒙山县| 吉水县| 红桥区| 旌德县| 汪清县| 无极县| 米泉市| 兴文县| 含山县| 八宿县| 北票市| 南丰县| 深水埗区| 新绛县| 成武县| 绵阳市| 江达县| 西充县| 康定县| 罗甸县| 碌曲县| 大安市| 台东市| 密云县| 视频| 固始县| 凤冈县| 长宁区| 青海省| 鞍山市| 津市市| 广汉市| 万山特区| 南投市| 靖远县|