PHP Image Verification Code multiple instances

Source: Internet
Author: User
  1. File Header ...

  2. Header ("Content-type:image/png");
  3. Create True Color white paper
  4. $im = @imagecreatetruecolor (a) or Die ("Build image Failed");
  5. Get background color
  6. $background _color = imagecolorallocate ($im, 255, 255, 255);
  7. Fill the background color (this thing is like oil barrels)
  8. Imagefill ($im, 0,0, $background _color);
  9. Get border color
  10. $border _color = imagecolorallocate ($im, 200,200,200);
  11. Draw a rectangle, border color 200,200,200
  12. Imagerectangle ($im, 0,0,49,19, $border _color);

  13. Line up background, full screen with 1 or 0

  14. for ($i =2; $i <18; $i + +) {
  15. Get random tint
  16. $line _color = Imagecolorallocate ($im, Rand (200,255), Rand (200,255), Rand (200,255));
  17. Draw Line
  18. Imageline ($im, 2, $i, $i, $line _color);
  19. }

  20. Set Font size

  21. $font _size=12;

  22. Set up the printed text

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

  26. Get a 1th random text

  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. Get a 2nd random text

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

  34. Get a 3rd random text

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

  38. Get a 4th random text

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

  42. Write Random string

  43. for ($i =0; $i <4; $i + +) {
  44. Get a random darker color
  45. $text _color = Imagecolorallocate ($im, Rand (50,180), Rand (50,180), Rand (50,180));
  46. Drawing text
  47. Imagechar ($im, $font _size, $imstr [$i] ["X"], $imstr [$i] ["Y"], $imstr [$i] ["s"], $text _color);
  48. }

  49. Show pictures

  50. Imagepng ($im);
  51. Destroying pictures
  52. Imagedestroy ($im);
  53. ?>

Copy Code

Example 2, a beautiful PHP image verification code example complete code:

  1. /*
  2. * @Author FY
  3. */
  4. $imgwidth = 100; Picture width
  5. $imgheight = 40; Picture 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, 0,0, $while); Fill image
  13. Get 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);//All lowercase, mainly for case insensitive
  21. Random draw points, has been changed to draw the stars
  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. Randomly draw lines, number of lines = number of characters (whatever)
  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); Calculating character distances
  34. $_y=intval ($imgheight *0.7); The character is displayed in the position of picture 70%
  35. for ($i =0; $i<>
  36. $randcolor =imagecolorallocate ($im, Mt_rand (0,150), Mt_rand (0,150), Mt_rand (0,150));
  37. Imagestring ($im, 5, $j, 5, $imgstr [$i], $color 3);
  38. Imagettftext (Resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, String $text)
  39. Imagettftext ($im, $fontsize, Mt_rand ( -30,30), $i *$_x+3,$_y, $randcolor, $font, $authstr [$i]);
  40. }
  41. Generate images
  42. Header ("Content-type:image/png");
  43. Imagepng ($im);
  44. Imagedestroy ($im);
Copy Code

Example 3,PHP5 Image captcha example code

PHP5 generates an example of a picture verification code.

Need to use PHP GD library function: 1,imagecreatetruecolor-----Create a true color image imagecreatetruecolor (int x_size,int y_size)//x is wide, y means high 2,imagecolorallocate assigns a color (palette) imagecolorallocate (Resource Image,int red,int green,int blue) to an image//red,green, Blue----RGB 3,imagestring drawing function iamgestring (resource image,font,int x,int y, content, color); 4, output function The header of PHP is the action that defines the head, PHP5 supports Type 3 in: 1,content-type:xxxx/yyyy 2,location:xxxx:yyyy/zzzz 3,status:nnn xxxxxx xxxx/yyyy indicates the type of content file such as: Image/gif Image/jpeg Image/png Example: Header ("Content-type:image/jpeg") in 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,imagettf Text with font write function 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, to convert the text to UTF-8 format 9, the random function 1,rand ([int min,int max])//rand (1,4) generates 1-4 of the number 2, Dechex (Ten inNumber)//Convert to Hex

To generate a verification code: generate a random number--Create a picture--a random number as a picture--saved in session

Enter the CAPTCHA example 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. Validation of adding Chinese
  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

login.php

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