Rumah > pembangunan bahagian belakang > tutorial php > PHP验证码文件类

PHP验证码文件类

WBOY
Lepaskan: 2016-07-25 08:42:24
asal
1038 orang telah melayarinya
  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 {
  53. $this->mDisturbColor = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,255), rand(0,255));
  54. imagesetpixel($this->mCheckImage,rand(2,128),rand(2,38),$this->mDisturbColor);
  55. }
  56. }
  57. /**
  58. *
  59. * @brief 设置验证码图片的大小
  60. *
  61. * @param $width 宽
  62. *
  63. * @param $height 高
  64. *
  65. */
  66. public function SetCheckImageWH($width,$height)
  67. {
  68. if($width==''||$height=='')return false;
  69. $this->mCheckImageWidth = $width;
  70. $this->mCheckImageHeight = $height;
  71. return true;
  72. }
  73. /**
  74. *
  75. * @brief 在验证码图片上逐个画上验证码
  76. *
  77. */
  78. private function WriteCheckCodeToImage()
  79. {
  80. for ($i=0;$imCheckCodeNum;$i++)
  81. {
  82. $bg_color = imagecolorallocate ($this->mCheckImage, rand(0,255), rand(0,128), rand(0,255));
  83. $x = floor($this->mCheckImageWidth/$this->mCheckCodeNum)*$i;
  84. $y = rand(0,$this->mCheckImageHeight-15);
  85. imagechar ($this->mCheckImage, 5, $x, $y, $this->mCheckCode[$i], $bg_color);
  86. }
  87. }
  88. /**
  89. *
  90. * @brief 输出验证码图片
  91. *
  92. */
  93. public function OutCheckImage()
  94. {
  95. $this ->OutFileHeader();
  96. $this ->CreateCheckCode();
  97. $this ->CreateImage();
  98. $this ->SetDisturbColor();
  99. $this ->WriteCheckCodeToImage();
  100. imagepng($this->mCheckImage);
  101. imagedestroy($this->mCheckImage);
  102. }
  103. }
  104. $c_check_code_image = new CCheckCodeFile();
  105. //$c_check_code_image ->SetCheckImageWH(100,50);//设置显示验证码图片的尺寸
  106. $c_check_code_image ->OutCheckImage();
  107. ?>
复制代码

验证码, PHP


Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan