Encapsulation of a php verification code

Source: Internet
Author: User
Encapsulation of a php verification code

  1. /**

  2. * Verification code
  3. * Edit bbs.it-home.org
  4. */
  5. Class ValidationCode {
  6. Private $ width;
  7. Private $ height;
  8. Private $ codeNum;
  9. Private $ image; // image resources
  10. Private $ disturbColorNum;
  11. Private $ checkCode;
  12. Function _ construct ($ width = 80, $ height = 20, $ codeNum = 4 ){
  13. $ This-> width = $ width;
  14. $ This-> height = $ height;
  15. $ This-> codeNum = $ codeNum;
  16. $ This-> checkCode = $ this-> createCheckCode ();
  17. $ Number = floor ($ width * $ height/15 );

  18. If ($ number> 240-$ codeNum ){

  19. $ This-> disturbcolornum= 240-$ codeNum;
  20. } Else {
  21. $ This-> disturbColorNum = $ number;
  22. }

  23. }

  24. // Output the image to the browser by accessing this method
  25. Function showImage ($ fontFace = ""){
  26. // Step 1: create an image background
  27. $ This-> createImage ();
  28. // Step 2: Set interference elements
  29. $ This-> setDisturbColor ();
  30. // Step 3: randomly draw text from the image
  31. $ This-> outputText ($ fontFace );
  32. // Step 4: output the image
  33. $ This-> outputImage ();
  34. }

  35. // Obtain the randomly created verification code string by calling this method

  36. Function getCheckCode (){
  37. Return $ this-> checkCode;
  38. }
  39. Private function createImage (){
  40. // Create image resources // bbs.it-home.org
  41. $ This-> image = imagecreatetruecolor ($ this-> width, $ this-> height );
  42. // Random background color
  43. $ BackColor = imagecolorallocate ($ this-> image, rand (225,255), rand (225,255), rand (225,255 ));
  44. // Add color to the background
  45. Imagefill ($ this-> image, 0, 0, $ backColor );
  46. // Set the border color
  47. $ Border = imagecolorallocate ($ this-> image, 0, 0, 0 );
  48. // Draw a rectangular border
  49. Imagerectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ border );
  50. }
  51. Private function setDisturbColor (){
  52. For ($ I = 0; $ I <$ this-> disturbColorNum; $ I ++ ){
  53. $ Color = imagecolorallocate ($ this-> image, rand (0,255), rand (0,255), rand (0,255 ));
  54. Imagesetpixel ($ this-> image, rand (1, $ this-> width-2), rand (1, $ this-> height-2), $ color );
  55. }
  56. For ($ I = 0; $ I <10; $ I ++ ){
  57. $ Color = imagecolorallocate ($ this-> image, rand (200,255), rand (200,255), rand (200,255 ));
  58. Imagearc ($ this-> image, rand (-10, $ this-> width), rand (-10, $ this-> height), rand (30,300 ), rand (20,200), 55, 44, $ color );
  59. }
  60. }
  61. Private function createCheckCode (){
  62. // Here the random code is mainly generated, starting from 2 to distinguish between 1 and l
  63. $ Code = "23456789 abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ ";
  64. $ String = '';
  65. For ($ I = 0; $ I <$ this-> codeNum; $ I ++ ){
  66. $ Char = $ code {rand (0, strlen ($ code)-1 )};
  67. $ String. = $ char;
  68. }
  69. Return $ string;
  70. }
  71. Private function outputText ($ fontFace = ""){
  72. For ($ I = 0; $ I <$ this-> codeNum; $ I ++ ){
  73. $ Fontcolor = imagecolorallocate ($ this-> image, rand (0,128), rand (0,128), rand (0,128 ));
  74. If ($ fontFace = ""){
  75. $ Fontsize = rand (3, 5 );
  76. $ X = floor ($ this-> width/$ this-> codeNum) * $ I + 3;
  77. $ Y = rand (0, $ this-> height-15 );
  78. Imagechar ($ this-> image, $ fontsize, $ x, $ y, $ this-> checkCode {$ I}, $ fontcolor );
  79. } Else {
  80. $ Fontsize = rand (12, 16 );
  81. $ X = floor ($ this-> width-8)/$ this-> codeNum) * $ I + 8;
  82. $ Y = rand ($ fontSize + 5, $ this-> height );
  83. Imagettftext ($ this-> image, $ fontsize, rand (-30, 30), $ x, $ y, $ fontcolor, $ fontFace, $ this-> checkCode {$ I });
  84. }
  85. }
  86. }

  87. // Output image information

  88. Private function outputImage (){
  89. If (imagetypes () & IMG_GIF ){
  90. Header ("Content-Type: image/gif ");
  91. Imagepng ($ this-> image );
  92. } Else if (imagetypes () & IMG_JPG ){
  93. Header ("Content-Type: image/jpeg ");
  94. Imagepng ($ this-> image); // bbs.it-home.org
  95. } Else if (imagetypes () & IMG_PNG ){
  96. Header ("Content-Type: image/png ");
  97. Imagepng ($ this-> image );
  98. } Else if (imagetypes () & IMG_WBMP ){
  99. Header ("Content-Type: image/vnd. wap. wbmp ");
  100. Imagepng ($ this-> image );
  101. } Else {
  102. Die ("PHP does not support Image creation ");
  103. }
  104. }
  105. Function _ destruct (){
  106. Imagedestroy ($ this-> image );
  107. }
  108. }
  109. ?>

Call example of the verification code class:

  1. Session_start ();
  2. Include "validationcode. class. php ";
  3. $ Code = new ValidationCode (80, 20, 4 );
  4. $ Code-> showImage (); // output to the page for registration or login
  5. $ _ SESSION ["code"] = $ code-> getCheckCode (); // Save the verification code to the server.
  6. ?>

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.