首頁 >後端開發 >php教程 >PHP驗證碼檔案類

PHP驗證碼檔案類

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

驗證碼, PHP


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn