Php image verification code multiple instances

Source: Internet
Author: User
Php image verification code multiple instances

  1. // File header...

  2. Header ("Content-type: image/png ");
  3. // Create white paper with true color
  4. $ Im = @ imagecreatetruecolor (50, 20) or die ("image creation failed ");
  5. // Obtain the background color
  6. $ Background_color = imagecolorallocate ($ instant, 255,255,255 );
  7. // Fill in the background color (this is similar to a barrel)
  8. Imagefill ($ im, 0, 0, $ background_color );
  9. // Obtain the border color
  10. $ Border_color = imagecolorallocate ($ im, 200,200,200 );
  11. // Draw a rectangle with a border color of 200,200,200
  12. Imagerectangle ($ im, $ border_color );

  13. // Show off the background line by line. use 1 or 0 in full screen.

  14. For ($ I = 2; $ I <18; $ I ++ ){
  15. // Obtain random light colors
  16. $ Line_color = imagecolorallocate ($ im, rand (200,255), rand (200,255), rand (200,255 ));
  17. // Draw a line
  18. Imageline ($ im, 2, $ I, 47, $ I, $ line_color );
  19. }

  20. // Set the font size

  21. $ Font_size = 12;

  22. // Set the printed text

  23. $ Str [0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
  24. $ Str [1] = "abcdefghijklmnopqrstuvwxyz ";
  25. $ Str [2] = "01234567891234567890123456 ";

  26. // Obtain 1st random texts

  27. $ Imstr [0] ["s"] = $ Str [rand (0, 2)] [rand (0, 25)];
  28. $ Imstr [0] ["x"] = rand (2, 5 );
  29. $ Imstr [0] ["y"] = rand (1, 4 );

  30. // Obtain 2nd random texts

  31. $ Imstr [1] ["s"] = $ Str [rand (0, 2)] [rand (0, 25)];
  32. $ Imstr [1] ["x"] = $ imstr [0] ["x"] + $ font_size-1 + rand );
  33. $ Imstr [1] ["y"] = rand (1, 3 );

  34. // Obtain 3rd random texts

  35. $ Imstr [2] ["s"] = $ Str [rand (0, 2)] [rand (0, 25)];
  36. $ Imstr [2] ["x"] = $ imstr [1] ["x"] + $ font_size-1 + rand );
  37. $ Imstr [2] ["y"] = rand (1, 4 );

  38. // Obtain 4th random texts

  39. $ Imstr [3] ["s"] = $ Str [rand (0, 2)] [rand (0, 25)];
  40. $ Imstr [3] ["x"] = $ imstr [2] ["x"] + $ font_size-1 + rand );
  41. $ Imstr [3] ["y"] = rand (1, 3 );

  42. // Write random strings

  43. For ($ I = 0; $ I <4; $ I ++ ){
  44. // Obtain random darker colors
  45. $ Text_color = imagecolorallocate ($ im, rand (50,180), rand (50,180), rand (50,180 ));
  46. // Draw text
  47. Imagechar ($ im, $ font_size, $ imstr [$ I] ["x"], $ imstr [$ I] ["y"], $ imstr [$ I] ["s"], $ text_color );
  48. }

  49. // Display the image

  50. Imagepng ($ im );
  51. // Destroy the image
  52. Imagedestroy ($ im );
  53. ?>

Example 2: complete code of the beautiful PHP image verification code instance:

  1. /*
  2. * @ Author fy
  3. */
  4. $ Imgwidth = 100; // image width
  5. $ Imgheight = 40; // Image height
  6. $ Codelen = 4; // verification code length
  7. $ Fontsize = 20; // font size
  8. $ Charset = 'abcdefghkmnprstuvwxyzabcdefghkmnprstuvwxyz23456789 ';
  9. $ Font = 'fonts/segoesc. ttf ';
  10. $ Im = imagecreatetruecolor ($ imgwidth, $ imgheight );
  11. $ While = imageColorAllocate ($ im, 255,255,255 );
  12. Imagefill ($ im, $ while); // fill the image
  13. // Obtain the string
  14. $ Authstr = '';
  15. $ _ Len = strlen ($ charset)-1;
  16. For ($ I = 0; $ I <$ codelen; $ I ++ ){
  17. $ Authstr. = $ charset [mt_rand (0, $ _ len)];
  18. }
  19. Session_start ();
  20. $ _ SESSION ['scode'] = strtolower ($ authstr); // Convert all to lowercase, mainly to be case insensitive
  21. // Randomly draw a point, which has been changed to star
  22. For ($ I = 0; $ I <$ imgwidth; $ I ++ ){
  23. $ Randcolor = imageColorallocate ($ im, mt_rand (200,255), mt_rand (200,255), mt_rand (200,255 ));
  24. Imagestring ($ im, mt_rand (1, 5), mt_rand (0, $ imgwidth), mt_rand (0, $ imgheight), '*', $ randcolor );
  25. // Imagesetpixel ($ im, mt_rand (0, $ imgwidth), mt_rand (0, $ imgheight), $ randcolor );
  26. }
  27. // Draw lines randomly. the number of lines = the number of characters (casual)
  28. For ($ I = 0; $ I <$ codelen; $ I ++)
  29. {
  30. $ Randcolor = imagecolorallocate ($ im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
  31. Imageline ($ im, 0, mt_rand (0, $ imgheight), $ imgwidth, mt_rand (0, $ imgheight), $ randcolor );
  32. }
  33. $ _ X = intval ($ imgwidth/$ codelen); // calculates the character distance.
  34. $ _ Y = intval ($ imgheight * 0.7); // the position where the character is displayed on the image 70%.
  35. For ($ I = 0; $ I $ Randcolor = imagecolorallocate ($ im, mt_rand (0,150), mt_rand (0,150), mt_rand (0,150 ));
  36. // Imagestring ($ im, 5, $ j, 5, $ imgstr [$ I], $ color3 );
  37. // Imagettftext (resource $ image, float $ size, float $ angle, int $ x, int $ y, int $ color, string $ fontfile, string $ text)
  38. Imagettftext ($ im, $ fontsize, mt_rand (-30,30), $ I * $ _ x + 3, $ _ y, $ randcolor, $ font, $ authstr [$ I]);
  39. }
  40. // Generate an image
  41. Header ("content-type: image/PNG ");
  42. ImagePNG ($ im );
  43. ImageDestroy ($ im );

Example 3: php5 image verification code

Php5 generates an image verification code.

You need to use the php GD library function: 1, imagecreatetruecolor ----- create a true color image imagecreatetruecolor (int x_size, int y_size) // x indicates width, and y indicates height 2, imagecolorallocate allocates color (color palette) to an image imagecolorallocate (resource image, int red, int green, int blue) // red, green, blue ---- three primary colors 3, imagestring plotting function iamgestring (resource image, font, int x, int y, content, color); 4. the output function php header defines the header action, php5 supports three types: 1, Content-type: xxxx/yyyy 2, Location: xxxx: yyyy/zzzz 3, Status: nnn xxxxxx/yyyy indicates the type of the Content file, for example, image/gif image/jpeg image/png. example: header ("Content-type: image/jpeg ") the GD Library contains the imagejpeg (), imagegif (), imagepang () 5, and imageline linear function iamgeline (resource image, int x1, int y1, int x2, int y2, int color); image --- image x1 --- start coordinate y1 x2 --- end coordinate y2 6, imagesetpixel image point function imagesetpixel (resource image, int x, int y, int color) 7, imagettftext with font writing function imagettftext (resource image, float size, float angle, int x, int y, int color, string fontfile, string text) 8, php verification code to insert a Chinese method iconv ("gb2312", "UTF-8", "string"); // first, convert the text to UTF-8 format 9, random function 1, rand ([int min, int max]) // rand () generates numbers 1-4 2, dechex (decimal number) // converts to hexadecimal

To generate a verification code: generate a random number -- create an image -- write a random number as an image -- save it to the session

Enter the verification code example gdchek. php

  1. /*
  2. * Generate an image verification code
  3. * And open the template in the editor.
  4. */
  5. Session_start ();
  6. For ($ I = 0; $ I <4; $ I ++ ){
  7. $ Rand. = dechex (rand (); // generates a 4-digit random number containing a hexadecimal number.
  8. }
  9. $ _ SESSION [check_gd] = $ rand;
  10. $ Img = imagecreatetruecolor (); // create an image
  11. $ Bg = imagecolorallocate ($ img, 0); // The first generated background color
  12. $ Fc = imagecolorallocate ($ img, 255,255,255); // generated font color
  13. // Draw a line for the image
  14. For ($ I = 0; $ I <3; $ I ++ ){
  15. $ Te = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 ));
  16. Imageline ($ img, rand (0,15), 0,100, 30, $ te );
  17. }
  18. // Draw images
  19. For ($ I = 0; I I <200; $ I ++ ){
  20. $ Te = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 ));
  21. Imagesetpixel ($ img, rand () % 100, rand () % 30, $ te );
  22. }
  23. // First, convert the text to UTF-8 format
  24. // $ Str = iconv ("gb2312", "UTF-8", "heheh ");
  25. // Add Chinese verification
  26. // Smkai. ttf is a font file. in order to play a font role in other people's computers, put the file in the project root directory, you can download it, and the local C: \ WINDOWS \ Fonts has
  27. Imagettftext ($ img, 11,10, 20,20, $ fc, "simkai. ttf", "Hello ");
  28. // Write the string in the image
  29. // Imagestring ($ img, rand (), $ rand, $ fc );
  30. // Output image
  31. Header ("Content-type: image/jpeg ");
  32. Imagejpeg ($ img );
  33. ?>

Login. php

  1. /*
  2. *
  3. *
  4. */
  5. Session_start ();
  6. If ($ _ POST [sub]) {
  7. // Determine whether the verification code is the same
  8. If ($ _ POST [gd_pic] ==$ _ SESSION [check_gd]) {
  9. Echo "verification successful! ";
  10. } Else {
  11. Echo "incorrect verification code ";
  12. }
  13. }
  14. ?>

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.