PHP Random Verification Code Image generation example detailed

Source: Internet
Author: User
  1. /** Default Home **/

  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 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 background color

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

  22. Get numbers randomly

  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 Main text information
  31. Imagestring ($im, 6, ($i *10) +3, rand (0,6), $random, $colors [rand (0, COUNT ($colors)-1)]);
  32. }

  33. Add Random noise

  34. for ($i =0; $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 verification Code in $_session

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

  40. Output a picture and release the cache

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

Copy Code

Example 2, a class that generates a random string and a validation code for a class of random strings and Authenticode.

The following code implementation, the main do can be very good to distinguish between a get_code (), another create_check_image (), the output image directly after the call, session () to take the verification code directly Get_code (). Session_star () must be placed at the front when using the session.

Full code:

  1. Class Randcheckcode

  2. {
  3. /* Function Name: Get_code ()
  4. * Function: Get random string
  5. Parameters
  6. 1, (int) $length = #随机字符长度
  7. 2, (int) $mode = 0 #随机字符类型,
  8. 0 for case English and numbers, 1 for numbers, 2 for lowercase letters, 3 for uppercase letters,
  9. 4 is uppercase and lowercase, 5 is capital letter and number, 6 is lowercase and number
  10. * Return: The obtained string
  11. */
  12. function Get_code ($length =32, $mode =0)//Get random verification code functions
  13. {
  14. Switch ($mode)
  15. {
  16. Case ' 1 ':
  17. $str = ' 123456789 ';
  18. Break
  19. Case ' 2 ':
  20. $str = ' abcdefghijklmnopqrstuvwxyz ';
  21. Break
  22. Case ' 3 ':
  23. $str = ' abcdefghijklmnopqrstuvwxyz ';
  24. Break
  25. Case ' 4 ':
  26. $str = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ';
  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);//Generate a random number between 0 and $len
  43. $num =mt_rand (0, $len);//Generate a random number between 0 and $len
  44. $checkstr. = $str [$num];

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

  48. /** function Name: Create_check_image ()

  49. Function: Produces a picture of a checksum code
  50. Parameter: $checkcode: Check code string
  51. Return value: Returns the picture
  52. */
  53. function Create_check_image ($checkcode)//produce a
  54. {
  55. $im =imagecreate (65,22);//produce a picture
  56. $black =imagecolorallocate ($im, 0,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);//In the $im image coordinates 30,30 (the upper left corner of the image is 0,0) with the $gray color to perform the area fill (that is, with 30, 30 points of the same color and adjacent points will be filled)

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

  61. for ($i =0; $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),///on $im image with $randcolor color in (rand ()%70,rand ()%30) coordinates (the upper left corner of the image is 0,0) Draw a dot on the
  65. }
  66. Header ("Content-type:image/png");
  67. Imagepng ($im);//Export the image to a browser or file in PNG format
  68. Imagedestroy ($im);//Destroy 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. ?>

Copy 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.