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

Verification code file class

WBOY
WBOYOriginal
2016-07-25 09:11:191018browse
Verification code file class
  1. /**
  2. * @file
  3. * @version 1.0
  4. * @author Wanglangzi
  5. * @date 2006-3-30
  6. * @email [email]sxf02615@163.com[/email]
  7. * @brief Verification code file class
  8. *
  9. */
  10. ?>
  11. class CCheckCodeFile
  12. {
  13. //Verification code digits
  14. private $mCheckCodeNum = 4;
  15. //Generate Verification code
  16. private $mCheckCode = '';
  17. //Picture of verification code
  18. private $mCheckImage = '';
  19. //Interference pixel
  20. private $mDisturbColor = '';
  21. //Picture width of verification code
  22. private $mCheckImageWidth = '80';
  23. //Verification code image width
  24. private $mCheckImageHeight = '20';
  25. /**
  26. *
  27. * @brief output header
  28. *
  29. */
  30. private function OutFileHeader()
  31. {
  32. header ("Content -type: image/png");
  33. }
  34. /**
  35. *
  36. * @brief generates verification code
  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 generates verification code image
  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 Set the interference pixels of the image
  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 Set the size of the verification code image
  71. *
  72. * @param $width width
  73. *
  74. * @param $height 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 Draw the verification codes one by one on the verification code picture
  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 Output verification code picture
  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);//Set the size of the verification code image
  117. $c_check_code_image -> ;OutCheckImage();
  118. ?>
Copy code


Statement:
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