PHP verification code file-php Tutorial

Source: Internet
Author: User
PHP verification code file

  1. Class CCheckCodeFile
  2. {
  3. // Number of digits of the verification code
  4. Private $ mCheckCodeNum = 4;
  5. // Generated verification code
  6. Private $ mCheckCode = '';
  7. // Verification code Image
  8. Private $ mCheckImage = '';
  9. // Interference pixel
  10. Private $ mDisturbColor = '';
  11. // The image width of the verification code
  12. Private $ mCheckImageWidth = '80 ';
  13. // The image width of the verification code
  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 generate the 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 generate a 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: sets the interference pixels of an 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 (), $ this-> mDisturbColor );
  56. }
  57. }
  58. /**
  59. *
  60. * @ Brief: set the size of the verification code image.
  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 Upload the verification code one by one on the verification code image.
  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 outputs the verification code Image
  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 (, 50); // you can specify the image size of the verification code.
  107. $ C_check_code_image-> OutCheckImage ();
  108. ?>


Verification code, PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.