Php verification code

Source: Internet
Author: User
Php verification code

  1. /**
  2. * Generic verification code class img. php
  3. * Version: V0.1
  4. * Bbs.it-home.org 2013/3/1
  5. */
  6. Class ValidateCode {
  7. Private $ charset = "abcdefghizklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; // random factor
  8. Private $ code; // verification code text
  9. Private $ codelen = 4; // several texts are displayed in the verification code
  10. Private $ width = 130; // verification code width
  11. Private $ height = 50; // verification code height
  12. Private $ img; // verification code resource handle
  13. Private $ font; // The specified font.
  14. Private $ fontsize = 20; // The specified font size
  15. Private $ fontcolor; // random font color
  16. // Font of the constructor class
  17. Public function _ construct (){
  18. $ This-> font = ROOT_PATH. '/font/elephant. ttf ';
  19. }
  20. // Create 4 random codes
  21. Private function createCode (){
  22. $ _ Leng = strlen ($ this-> charset );
  23. For ($ I = 1; $ I <= $ this-> codelen; $ I ++ ){
  24. $ This-> code. = $ this-> charset [mt_rand (0, $ _ leng)];
  25. }
  26. Return $ this-> code;
  27. }
  28. // Create a background
  29. Private function createBg (){
  30. // Create a canvas for a resource jubing
  31. $ This-> img = imagecreatetruecolor ($ this-> width, $ this-> height );
  32. // Background color
  33. $ Color = imagecolorallocate ($ this-> img, mt_rand (157,255), mt_rand (157,255), mt_rand (157,255 ));
  34. // Draw a rectangle
  35. Imagefilledrectangle ($ this-> img, 0, $ this-> height, $ this-> width, 0, $ color );
  36. }
  37. // Create a font
  38. Private function createFont (){
  39. $ _ X = ($ this-> width/$ this-> codelen); // font length
  40. For ($ I = 0; $ I <$ this-> codelen; $ I ++ ){
  41. // Text color
  42. $ Color = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156 ));
  43. // Resource handle font size skew font length font height font color font specific text
  44. Imagettftext ($ this-> img, $ this-> fontsize, mt_rand (-30, 30), $ _ x * $ I + mt_rand (1.4), $ this-> height, $ color, $ this-> font, $ this-> code [$ I]);
  45. }
  46. }
  47. // Random line
  48. Private function createLine (){
  49. // Random line
  50. For ($ I = 0; $ I <6; $ I ++ ){
  51. $ Color = imagecolorallocate ($ this-> img, mt_rand (0,156), mt_rand (0,156), mt_rand (0,156 ));
  52. Imageline ($ this-> img, mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height), $ color );
  53. }
  54. // Random snowflake
  55. For ($ I = 0; $ I <45; $ I ++ ){
  56. $ Color = imagecolorallocate ($ this-> img, mt_rand (220,255), mt_rand (220,255), mt_rand (220,255 ));
  57. Imagestring ($ this-> img, mt_rand (1, 5), mt_rand (0, $ this-> width), mt_rand (0, $ this-> height ),'*', $ color );
  58. }
  59. }
  60. // Output background
  61. Private function outPut (){
  62. // Generate the header
  63. Header ('contenttype: img/png ');
  64. // Output image
  65. Imagepng ($ this-> img );
  66. // Destroy the result set
  67. Imagedestroy ($ this-> img );
  68. }
  69. // Output externally
  70. Public function doimg (){
  71. // Load the background
  72. $ This-> createBg ();
  73. // Load the file
  74. $ This-> createCode ();
  75. // Load the line
  76. $ This-> createLine ();
  77. // Load the font
  78. $ This-> createFont ();
  79. // Load the background
  80. $ This-> outPut ();
  81. }
  82. // Obtain the verification code
  83. Public function getCode (){
  84. Return strtolower ($ this-> code );
  85. }
  86. }
  87. ?>

Call example: index. php recommends several articles on php verification code: php random verification code php generates random verification code (text) use php to generate a verification code with a snowflake background. a verification code written by php to generate a dynamic image verification code.

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.