首页 >后端开发 >php教程 >验证码文件类

验证码文件类

WBOY
WBOY原创
2016-07-25 09:11:19995浏览
验证码文件类
  1. /**
  2. * @file
  3. * @version 1.0
  4. * @author 网海浪子
  5. * @date 2006-3-30
  6. * @email [email]sxf02615@163.com[/email]
  7. * @brief 验证码文件类
  8. *
  9. */
  10. ?>
  11. class CCheckCodeFile
  12. {
  13. //验证码位数
  14. private $mCheckCodeNum = 4;
  15. //产生的验证码
  16. private $mCheckCode = '';
  17. //验证码的图片
  18. private $mCheckImage = '';
  19. //干扰像素
  20. private $mDisturbColor = '';
  21. //验证码的图片宽度
  22. private $mCheckImageWidth = '80';
  23. //验证码的图片宽度
  24. private $mCheckImageHeight = '20';
  25. /**
  26. *
  27. * @brief 输出头
  28. *
  29. */
  30. private function OutFileHeader()
  31. {
  32. header ("Content-type: image/png");
  33. }
  34. /**
  35. *
  36. * @brief 产生验证码
  37. *
  38. */
  39. private function CreateCheckCode()
  40. {
  41. $this->mCheckCode = strtoupper(substr(md5(rand()),0,$this->mCheckCodeNum));
  42. return $this->mCheckCode;
  43. }
  44. /**
  45. *
  46. * @brief 产生验证码图片
  47. *
  48. */
  49. private function CreateImage()
  50. {
  51. $this->mCheckImage = @imagecreate ($this->mCheckImageWidth,$this->mCheckImageHeight);
  52. imagecolorallocate ($this->mCheckImage, 200, 200, 200);
  53. return $this->mCheckImage;
  54. }
  55. /**
  56. *
  57. * @brief 设置图片的干扰像素
  58. *
  59. */
  60. private function SetDisturbColor()
  61. {
  62. for ($i=0;$i {
  63. $this->mDisturbColor = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,255), rand(0,255));
  64. imagesetpixel($this->mCheckImage,rand(2,128),rand(2,38),$this->mDisturbColor);
  65. }
  66. }
  67. /**
  68. *
  69. * @brief 设置验证码图片的大小
  70. *
  71. * @param $width 宽
  72. *
  73. * @param $height 高
  74. *
  75. */
  76. public function SetCheckImageWH($width,$height)
  77. {
  78. if($width==''||$height=='')return false;
  79. $this->mCheckImageWidth = $width;
  80. $this->mCheckImageHeight = $height;
  81. return true;
  82. }
  83. /**
  84. *
  85. * @brief 在验证码图片上逐个画上验证码
  86. *
  87. */
  88. private function WriteCheckCodeToImage()
  89. {
  90. for ($i=0;$imCheckCodeNum;$i++)
  91. {
  92. $bg_color = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,128), rand(0,255));
  93. $x = floor($this->mCheckImageWidth/$this->mCheckCodeNum)*$i;
  94. $y = rand(0,$this->mCheckImageHeight-15);
  95. imagechar ($this->mCheckImage, 5, $x, $y, $this->mCheckCode[$i], $bg_color);
  96. }
  97. }
  98. /**
  99. *
  100. * @brief 输出验证码图片
  101. *
  102. */
  103. public function OutCheckImage()
  104. {
  105. $this ->OutFileHeader();
  106. $this ->CreateCheckCode();
  107. $this ->CreateImage();
  108. $this ->SetDisturbColor();
  109. $this ->WriteCheckCodeToImage();
  110. imagepng($this->mCheckImage);
  111. imagedestroy($this->mCheckImage);
  112. }
  113. }
  114. $c_check_code_image = new CCheckCodeFile();
  115. //$c_check_code_image ->SetCheckImageWH(100,50);//设置显示验证码图片的尺寸
  116. $c_check_code_image ->OutCheckImage();
  117. ?>
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn