Example code of php5 image verification code

Source: Internet
Author: User
Example code of php5 image verification code

  1. Header ("Content-type: image/jpeg ")

The GD Library has the corresponding image types: imagejpeg (), imagegif (), imagepang ()

5. 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: imagesetpixel (resource image, int x, int y, int color)

7. imagettftext: the write function imagettftext with font (resource image, float size, float angle, int x, int y, int color, string fontfile, string text)

8. the php verification code is inserted into the Chinese iconv ("gb2312", "UTF-8", "string"); // you must first convert the text into UTF-8 format

9. random functions 1, rand ([int min, int max]) // rand () generate numbers 1-4 2, dechex (decimal number) // Convert to hexadecimal

The above describes some common methods for gd library functions in php, as well as the method for inserting Chinese characters in the php verification code, when it comes to displaying Chinese characters in the php verification code, you can refer to the previous article about the PHP Chinese character verification code (example.

In addition, if you are interested, you can also study the implementation of php random verification code and php image verification code. it will be helpful for you to understand the example today.

To generate a verification code in php, follow these steps: generate a random number-create an image-write a random number as an image-and save it to the session.

1. enter the verification code 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 bbs.it-home.org
  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. ?>

2. log on to the login. php page.

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

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.