Home > Backend Development > PHP Tutorial > PHP verification code file class

PHP verification code file class

WBOY
Release: 2016-07-25 08:42:24
Original
1039 people have browsed it
  1. class CCheckCodeFile
  2. {
  3. //Number of verification code digits
  4. private $mCheckCodeNum = 4;
  5. //Generated verification code
  6. private $mCheckCode = '';
  7. //Verification code Picture
  8. private $mCheckImage = '';
  9. //Interference pixel
  10. private $mDisturbColor = '';
  11. //Verification code picture width
  12. private $mCheckImageWidth = '80';
  13. //Verification code picture Width
  14. private $mCheckImageHeight = '20';
  15. /**
  16. *
  17. * @brief output header
  18. *
  19. */
  20. private function OutFileHeader()
  21. {
  22. header ("Content-type: image/png");
  23. }
  24. /**
  25. *
  26. * @brief generates verification code
  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 generates verification code image
  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 Set the interference pixels of the image
  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 Set the size of the verification code image
  61. *
  62. * @param $width width
  63. *
  64. * @param $height 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 Draw the verification codes one by one on the verification code picture
  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 Output verification code picture
  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);//Set the size of the verification code image
  107. $c_check_code_image ->OutCheckImage();
  108. ?>
Copy code

Verification code, PHP


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template