当前位置:首页 > PHP教程 > php图像处理 > 列表

PHP验证码生成类完整代码

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-28 09:02:51 浏览: 评论:0 

本文章提供这款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. // 2. 定义随机获取字符串函数 
  24. private function codestr(){ 
  25. switch($this->Type){ 
  26.  
  27. case 1: // 类型为1 获取1-9随机数 
  28. $str = implode("",array_rand(range(0,9),$this->num)); 
  29. break
  30. case 2: // 类型为2 获取a-z随机小写字母 
  31. $str = implode("",array_rand(array_flip(range(a,z)),$this->num)); 
  32. break
  33. case 3: // 类型为3 获取数字,小写字母,大写字母 混合 
  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. 干扰点 
  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. 随机直线 弧线 
  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. }//开源代码phpfensi.com 
  108.  
  109. public function getCodeStr(){ 
  110. return $this->codestr;  
  111. ?> 

Tags: PHP验证码 PHP生成验证码

分享到: