GD library functions
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 to an image (color palette)
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. Output Functions
The php header defines the action of the header, and php5 supports three types:
1, Content-type: xxxx/yyyy
2, Location: xxxx: yyyy/zzzz
3, Status: nnn xxxxxx
Xxxx/yyyy indicates the content file type
For example, image/gif
Image/jpeg
Image/png
Example: header ("Content-type: image/jpeg ")
The GD library has the corresponding image type.
Imagejpeg (), imagegif (), imagepang ()
5. imageline line 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 point function
Imagesetpixel (resource image, int x, int y, int color)
7. imagettftext writing function with font
Imagettftext (resource image, float size, float angle, int x, int y, int color, string fontfile, string text)
8. How to insert the php verification code into Chinese
Iconv ("gb2312", "UTF-8", "string"); // convert the text to UTF-8
9. Random Functions
1, rand ([int min, int max]) // rand () generates numbers from 1 to 4
2, dechex (decimal number) // convert to hexadecimal
Verification Code steps:
Generate random number -- Create Image -- write random number as image -- save in session
Input verification code example
Gdchek. php
Copy codeThe Code is as follows:
<? 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
// 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 );
?>
Login. php
Copy codeThe Code is as follows:
<? Php
/*
*
*
*/
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 ";
}
}
?>
<Form action = "login. php" method = "POST">
Username: <input type = "text" name = "user"/> <br>
Password: <input type = "password" name = "pwd"/> <br>
Verification Code: <imput type = "text" name = "gd_pic"/> <br>
<Imput type = "submit" name = "sub" value = "submit"/>
</Form>