- 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
- /*
- * Generate an image verification code
- * And open the template in the editor.
- */
- Session_start ();
- For ($ I = 0; $ I <4; $ I ++ ){
- $ Rand. = dechex (rand (); // generates a 4-digit random number containing a hexadecimal number.
- }
- $ _ SESSION [check_gd] = $ rand;
- $ Img = imagecreatetruecolor (); // create an image
- $ Bg = imagecolorallocate ($ img, 0); // The first generated background color
- $ Fc = imagecolorallocate ($ img, 255,255,255); // generated font color
- // Draw a line for the image
- For ($ I = 0; $ I <3; $ I ++ ){
- $ Te = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 ));
- Imageline ($ img, rand (0,15), 0,100, 30, $ te );
- }
- // Draw images
- For ($ I = 0; I I <200; $ I ++ ){
- $ Te = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 ));
- Imagesetpixel ($ img, rand () % 100, rand () % 30, $ te );
- }
- // First, convert the text to UTF-8 format
- // $ Str = iconv ("gb2312", "UTF-8", "heheh ");
- // Add Chinese verification bbs.it-home.org
- // 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
- Imagettftext ($ img, 11,10, 20,20, $ fc, "simkai. ttf", "Hello ");
- // Write the string in the image
- // Imagestring ($ img, rand (), $ rand, $ fc );
- // Output image
- Header ("Content-type: image/jpeg ");
- Imagejpeg ($ img );
- ?>
2. log on to the login. php page.
- /*
- * Verify the logon and use the verification code.
- */
- Session_start ();
- If ($ _ POST [sub]) {
- // Determine whether the verification code is the same
- If ($ _ POST [gd_pic] ==$ _ SESSION [check_gd]) {
- Echo "verification successful! ";
- } Else {
- Echo "incorrect verification code ";
- }
- }
- ?>
|