Php random verification code image generation instance details

Source: Internet
Author: User
Php random verification code image generation instance details

  1. /** Default homepage **/

  2. Class DefaultController extends AppController
  3. {
  4. Public function index (){
  5. $ Len = 5;
  6. $ Str = "ABCDEFGHIJKLNMPQRSTUVWXYZ123456789 ";

  7. $ Im = imagecreatetruecolor (70, 20 );

  8. $ Bgc = imagecolorallocate ($ im, 255,255,255 );
  9. $ Bgtxt = imagecolorallocate ($ im, 220,220,220 );

  10. // Random color palette

  11. $ Colors = array (
  12. Imagecolorallocate ($ im, 255, 0, 0 ),
  13. Imagecolorallocate ($ im, 0,200, 0 ),
  14. Imagecolorallocate ($ im, 0, 0,255 ),
  15. Imagecolorallocate ($ im, 0, 0, 0 ),
  16. Imagecolorallocate ($ im, 255,128, 0 ),
  17. Imagecolorallocate ($ im, 255,208, 0 ),
  18. Imagecolorallocate ($ im, 98,186,245 ),
  19. );

  20. // Fill in the background color

  21. Imagefill ($ im, 0, 0, $ bgc );

  22. // Obtain random numbers

  23. $ Verify = "";
  24. While (strlen ($ verify) <$ len ){
  25. $ I = strlen ($ verify );
  26. $ Random = $ str [rand (0, strlen ($ str)];
  27. $ Verify. = $ random;

  28. // Draw background text

  29. Imagestring ($ im, 6, ($ I * 10) + 3, rand (0, 6), $ random, $ bgtxt );
  30. // Draw the main text information
  31. Imagestring ($ im, 6, ($ I * 10) + 3, rand (0, 6), $ random, $ colors [rand (0, count ($ colors) -1)]);
  32. }

  33. // Add random colors

  34. For ($ I = 0; I I <100; $ I ++ ){
  35. $ Color = imagecolorallocate ($ im, rand (50,220), rand (50,220), rand (50,220 ));
  36. Imagesetpixel ($ im, rand (0, 70), rand (0, 20), $ color );
  37. }

  38. // Save the verification code to $ _ SESSION

  39. Sess ("verify", $ verify );

  40. // Output the image and release the cache

  41. Header ('content-type: image/png ');
  42. Imagepng ($ im );
  43. Imagedestroy ($ im );
  44. }
  45. };
  46. ?>

Example 2: a php instance that generates a random string and verification code class.

The implementation of the following code can distinguish one get_code () and the other create_check_image (). the output image is called directly after the session () command, and get_code () is used to obtain the verification code. When using session, you must put session_star () at the beginning.

Complete code:

  1. Class RandCheckCode

  2. {
  3. /* Function name: get_code ()
  4. * Function: obtain a random string.
  5. * Parameters:
  6. 1. (int) $ length = 32 # random character length
  7. 2. (int) $ mode = 0 # random character type,
  8. 0 is a combination of uppercase and lowercase English letters and numbers, 1 is a number, 2 is a lowercase letter, 3 is a capital letter,
  9. 4: uppercase letters, 5: uppercase letters and numbers, 6: lowercase letters and numbers
  10. * Return: the obtained string.
  11. */
  12. Function get_code ($ length = 32, $ mode = 0) // Obtain the random verification code function
  13. {
  14. Switch ($ mode)
  15. {
  16. Case '1 ':
  17. $ Str = '000000 ';
  18. Break;
  19. Case '2 ':
  20. $ Str = 'abcdefghijklmnopqrstuvwxy ';
  21. Break;
  22. Case '3 ':
  23. $ Str = 'abcdefghijklmnopqrstuvwxy ';
  24. Break;
  25. Case '4 ':
  26. $ Str = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy ';
  27. Break;
  28. Case '5 ':
  29. $ Str = 'abcdefghijklmnopqrstuvwxyz1234567890 ';
  30. Break;
  31. Case '6 ':
  32. $ Str = 'abcdefghijklmnopqrstuvwxyz1234567890 ';
  33. Break;
  34. Default:
  35. $ Str = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890 ';
  36. Break;
  37. }
  38. $ Checkstr = '';
  39. $ Len = strlen ($ str)-1;
  40. For ($ I = 0; $ I <$ length; $ I ++)
  41. {
  42. // $ Num = rand (0, $ len); // generates a random number between 0 and $ len.
  43. $ Num = mt_rand (0, $ len); // generates a random number between 0 and $ len.
  44. $ Checkstr. = $ str [$ num];

  45. }
  46. Return $ checkstr;
  47. }

  48. /** Function name: create_check_image ()

  49. Function: generates an image of the verification code.
  50. Parameter: $ checkcode: verification code string
  51. Return value: returns the image.
  52. */
  53. Function create_check_image ($ checkcode) // Generate
  54. {
  55. $ Im = imagecreate (); // generate an image
  56. $ Black = imagecolorallocate ($ im, 0, 0); // background color
  57. $ White = imagecolorallocate ($ im, 255,255,255); // foreground color
  58. $ Gray = imagecolorallocate ($ im, 200,200,200 );
  59. Imagefill ($ im, 30, 30, $ gray); // The coordinates of the $ im image are 30, 30 (the upper left corner of the image is 0, 0) fill the area with the $ gray color (that is, the adjacent points will be filled with the same color as the 30, 30 points)

  60. Imagestring ($ im, 5, 8, 3, $ checkcode, $ white ); // use the $ white color to draw the string $ checkcode to the 8, 3 coordinates of the image represented by $ im (this is the coordinate of the upper left corner of the string, the upper left corner of the entire image is 0, 0), and 5 is the font size, the font can only be 1, 2, 3, 4, or 5. the built-in font is used.

  61. For ($ I = 0; I I <120; $ I ++)
  62. {
  63. $ Randcolor = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
  64. Imagesetpixel ($ im, rand () % 70, rand () % 30, $ randcolor); // use $ randcolor on $ im images at (rand () % 70, rand () % 30) coordinate (the upper left corner of the image is 0, 0 ).
  65. }
  66. Header ("Content-type: image/png ");
  67. Imagepng ($ im); // output the image to a browser or file in PNG format
  68. Imagedestroy ($ im); // destroy the image $ im
  69. }
  70. }
  71. /*
  72. $ Randcode = new RandCheckCode ();
  73. $ Checkstring = $ randcode-> get_code (5, 7 );
  74. $ Image = $ randcode-> create_check_image ($ checkstring );
  75. Echo $ image;
  76. */
  77. ?>

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.