PHP5 example code for a picture verification code

Source: Internet
Author: User
Tags imagejpeg
    1. Header ("Content-type:image/jpeg")
Copy Code

The GD library has the corresponding image type Imagejpeg (), Imagegif (), Imagepang ()

5,imageline Draw Line Function Iamgeline (resource image,int x1,int y1,int x2,int y2,int color); Image---Picture x1---start coordinates y1 x2---end coordinates y2

6,imagesetpixel Draw point function imagesetpixel (resource image,int x,int y,int color)

7,imagettftext write function with Font imagettftext (resource image,float size,float angle,int x,int y,int color,string fontfile,string Text

8,php verification code inserted in 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 (1,4) generates 1-4 of number 2, Dechex (decimal number)//converted to hexadecimal

The above introduces some common methods of the GD library function in PHP, as well as the method of inserting Chinese in PHP verification code, and referring to the problem of displaying Chinese in PHP verification code, we can refer to the previous PHP Chinese character Verification Code (example) of this article.

In addition, interested friends, can also study the PHP Random Verification Code and PHP image Verification code implementation method, for the understanding of today's example, will be very helpful.

In PHP, the steps to generate a CAPTCHA: Generate a random number--Create a picture--a random number as a picture--is saved in the session.

One, enter the verification code gdchek.php

  1. /*
  2. * Generate 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 (1,15)); Generates a 4-digit random number with 16 decimal digits
  8. }
  9. $_session[check_gd]= $rand;
  10. $img =imagecreatetruecolor (100,30); Create a picture
  11. $BG =imagecolorallocate ($img, 0,0,0); Background color is generated for the first time
  12. $FC =imagecolorallocate ($img, 255,255,255); The resulting font color
  13. Draw a line to a picture
  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 points for pictures
  19. for ($i =0; $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", "ha hehe");
  25. Join the Chinese verification bbs.it-home.org
  26. Smkai.ttf is a font file, in order to be in other people's computer can play the role of the font, put the file to the root of the project, can be downloaded, there is a native C:\WINDOWS\Fonts
  27. Imagettftext ($img, 11,10,20,20, $FC, "Simkai.ttf", "Hello Hello");
  28. Write the string in the picture
  29. Imagestring ($img, Rand (1,6), Rand (3,70), Rand (3,16), $rand, $FC);
  30. Output picture
  31. Header ("Content-type:image/jpeg");
  32. Imagejpeg ($IMG);
  33. ?>
Copy Code

2, login page login.php

    1. /*
    2. * Verify login, use the Verification code
    3. */
    4. Session_Start ();
    5. if ($_post[sub]) {
    6. Determine if the verification code is the same
    7. if ($_POST[GD_PIC]==$_SESSION[CHECK_GD]) {
    8. echo "Verified success! ";
    9. }else{
    10. echo "Verification code error";
    11. }
    12. }
    13. ?>
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.