- Header ("Content-type:image/jpeg")
Copy CodeThe 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
- /*
- * Generate Image Verification code
- * and open the template in the editor.
- */
- Session_Start ();
- for ($i =0; $i <4; $i + +) {
- $rand. =dechex (rand (1,15)); Generates a 4-digit random number with 16 decimal digits
- }
- $_session[check_gd]= $rand;
- $img =imagecreatetruecolor (100,30); Create a picture
- $BG =imagecolorallocate ($img, 0,0,0); Background color is generated for the first time
- $FC =imagecolorallocate ($img, 255,255,255); The resulting font color
- Draw a line to a picture
- 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 points for pictures
- for ($i =0; $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", "ha hehe");
- Join the Chinese verification bbs.it-home.org
- 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
- Imagettftext ($img, 11,10,20,20, $FC, "Simkai.ttf", "Hello Hello");
- Write the string in the picture
- Imagestring ($img, Rand (1,6), Rand (3,70), Rand (3,16), $rand, $FC);
- Output picture
- Header ("Content-type:image/jpeg");
- Imagejpeg ($IMG);
- ?>
Copy Code2, login page login.php
- /*
- * Verify login, use the Verification code
- */
- Session_Start ();
- if ($_post[sub]) {
- Determine if the verification code is the same
- if ($_POST[GD_PIC]==$_SESSION[CHECK_GD]) {
- echo "Verified success! ";
- }else{
- echo "Verification code error";
- }
- }
- ?>
Copy Code |