首页 > 语言 > PHP > 正文

PHP验证码生成类完整代码

2024-09-04 11:44:30
字体:大 中 小
来源:转载
供稿:网友

本文章提供这款php验证码生成类灵活好用,用户可以定义各个成员 有宽、高、画布、字数、类型、画类型同时我们只要修改 $Type就可以定义生成的是纯数字,纯小写字母,大小写数字混合,有需要的朋友可参考.

PHP验证码生成类完整代码如下:

  1. <?php 
  2. class Code{ 
  3.  
  4. // 1. 定义各个成员 有宽、高、画布、字数、类型、画类型 
  5.  
  6. private $width; //宽度 
  7. private $height; //高度 
  8. private $num; //验证码字数 
  9. private $imgType; //生成图片类型 
  10. private $Type; //字串类型 1,2,3 三个选项 1 纯数字 2 纯小写字母 3 大小写数字混合 
  11. private $hb; //画布 
  12. public $codestr; // 验证码字串 
  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.  
  24. // 2. 定义随机获取字符串函数 
  25. private function codestr(){ 
  26. switch($this->Type){ 
  27.  
  28. case 1: // 类型为1 获取1-9随机数 
  29. $str = implode("",array_rand(range(0,9),$this->num)); 
  30. break; 
  31. case 2: // 类型为2 获取a-z随机小写字母 
  32. $str = implode("",array_rand(array_flip(range(a,z)),$this->num)); 
  33. break; 
  34. case 3: // 类型为3 获取数字,小写字母,大写字母 混合 
  35. for($i=0;$i<$this->num;$i++){ 
  36. $m = rand(0,2); 
  37. switch($m){ 
  38. case 0: 
  39. $o = rand(48,57); 
  40. break; 
  41. case 1: 
  42. $o = rand(65,90); 
  43. break; 
  44. case 2: 
  45. $o = rand(97,122); 
  46. break;  
  47. } 
  48. $str .= sprintf("%c",$o); 
  49. } 
  50. break;  
  51. } 
  52.  
  53.  
  54. return $str;  
  55. } 
  56.  
  57.  
  58. // 3. 初始化画布图像资源 
  59. private function Hb(){ 
  60. $this->hb = imagecreatetruecolor($this->width,$this->height);  
  61. } 
  62.  
  63. // 4. 生成背景颜色 
  64. private function Bg(){ 
  65. return imagecolorallocate($this->hb,rand(130,250),rand(130,250),rand(130,250));  
  66. } 
  67.  
  68. // 5. 生成字体颜色 
  69. private function Font(){ 
  70. return imagecolorallocate($this->hb,rand(0,100),rand(0,100),rand(0,100));  
  71. } 
  72.  
  73. // 6. 填充背景颜色 
  74. private function BgColor(){ 
  75. imagefilledrectangle($this->hb,0,0,$this->width,$this->height,$this->Bg());  
  76. } 
  77.  
  78. // 7. 干扰点 
  79. private function ganrao(){ 
  80. $sum=floor(($this->width)*($this->height)/3); 
  81. for($i=0;$i<$sum;$i++){ 
  82. imagesetpixel($this->hb,rand(0,$this->width),rand(0,$this->height),$this->Bg());  
  83. } 
  84. } 
  85.  
  86. // 8. 随机直线 弧线 
  87. private function huxian(){ 
  88. for($i=0;$i<$this->num;$i++){ 
  89. 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());  
  90. }  
  91. } 
  92.  
  93. // 9. 写字 
  94. private function xiezi(){ 
  95. for($i=0;$i<$this->num;$i++){ 
  96. $x=ceil($this->width/$this->num)*$i;  
  97. $y=rand(1,$this->height-15); 
  98. imagechar($this->hb,5,$x+4,$y,$this->codestr[$i],$this->Font()); 
  99. }  
  100. } 
  101.  
  102. // 10. 输出 
  103. private function OutImg(){ 
  104. $shuchu="image".$this->imgType;  
  105. $header="Content-type:image/".$this->imgType; 
  106. if(function_exists($shuchu)){ 
  107. header($header); 
  108. $shuchu($this->hb);  
  109. }else{ 
  110. exit("GD库没有此类图像");  
  111. } 
  112. } 
  113.  
  114. // 11. 拼装 
  115. private function zuhe(){ 
  116. $this->Hb(); 
  117. $this->BgColor(); 
  118. $this->ganrao(); 
  119. $this->huxian(); 
  120. $this->xiezi(); 
  121. $this->OutImg();  
  122. }//开源代码Vevb.com 
  123.  
  124. public function getCodeStr(){ 
  125. return $this->codestr;  
  126. } 
  127. } 
  128. ?> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表