驗證碼檔案類

WBOY
發布: 2016-07-25 09:11:19
原創
987 人瀏覽過
验证码文件类
  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<=128;$i++)
  63. {
  64. $this->mDisturbColor = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,255), rand(0,255));
  65. imagesetpixel($this->mCheckImage,rand(2,128),rand(2,38),$this->mDisturbColor);
  66. }
  67. }
  68. /**
  69. *
  70. * @brief 设置验证码图片的大小
  71. *
  72. * @param $width 宽
  73. *
  74. * @param $height 高
  75. *
  76. */
  77. public function SetCheckImageWH($width,$height)
  78. {
  79. if($width==''||$height=='')return false;
  80. $this->mCheckImageWidth = $width;
  81. $this->mCheckImageHeight = $height;
  82. return true;
  83. }
  84. /**
  85. *
  86. * @brief 在验证码图片上逐个画上验证码
  87. *
  88. */
  89. private function WriteCheckCodeToImage()
  90. {
  91. for ($i=0;$i<=$this->mCheckCodeNum;$i++)
  92. {
  93. $bg_color = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,128), rand(0,255));
  94. $x = floor($this->mCheckImageWidth/$this->mCheckCodeNum)*$i;
  95. $y = rand(0,$this->mCheckImageHeight-15);
  96. imagechar ($this->mCheckImage, 5, $x, $y, $this->mCheckCode[$i], $bg_color);
  97. }
  98. }
  99. /**
  100. *
  101. * @brief 输出验证码图片
  102. *
  103. */
  104. public function OutCheckImage()
  105. {
  106. $this ->OutFileHeader();
  107. $this ->CreateCheckCode();
  108. $this ->CreateImage();
  109. $this ->SetDisturbColor();
  110. $this ->WriteCheckCodeToImage();
  111. imagepng($this->mCheckImage);
  112. imagedestroy($this->mCheckImage);
  113. }
  114. }
  115. $c_check_code_image = new CCheckCodeFile();
  116. //$c_check_code_image ->SetCheckImageWH(100,50);//设置显示验证码图片的尺寸
  117. $c_check_code_image ->OutCheckImage();
  118. ?>
复制代码


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板