Graph verification code (with Chinese characters) generated by GD Library in PHP5 _ PHP Tutorial

Source: Internet
Author: User
The GD Library in PHP5 generates a graphic verification code (with Chinese characters ). The graphic verification code generated using the GD Library in PHP5 is similar to the following one. use the GD library function to generate an image, and write the specified character imagecreatetruecolor on the image to create a true color image. use the GD Library in PHP5 to generate a graphic verification code.



Similar to the following:
1. use the GD library function to generate an image and write the specified character on the image
Imagecreatetruecolor: create a true color image
Imagecolorallocate allocates color to an image (color palette)
Imagestring draw character
Imageline draw line
Imagesetpixel
2. output image
Imagejpeg ($ img );
PHP implementation process, detailed comments in the code, not explained too much here
Verify. php

The code is as follows:


// 1. qi enables the gd library of the GD library to provide a series of APIs for image processing. the GD library can be used to process images or generate images.
// The GD library on the website is usually used to generate thumbnails, add watermarks to images, or generate reports on website data.
Session_start ();
// The GBK encoding string into a UTF-8 string, the first parameter is written GBK, because the php file stored in the host encoding is GBK encoding
// UTF-8 coding browser universal support, versatility, here is converted to UTF-8
$ Str = iconv ("GBK", "UTF-8", "all sentient beings, green mountains, mountains, scenic spots, and monuments will always be with you ");
If (! Is_string ($ str) |! Mb_check_encoding ($ str, "UTF-8 "))
{
Exit ("not a string or not UTF-8 ");
}
$ Zhongwenku_size;
// Obtain the length of the string by UTF-8 encoding
$ Zhongwenku_size = mb_strlen ($ str, "UTF-8 ");
// Import the preceding characters into the array
$ Zhongwenku = array ();
For ($ I = 0; $ I <$ zhongwenku_size; $ I ++)
{
$ Zhongwenku [$ I] = mb_substr ($ str, $ I, 1, "UTF-8 ");
}
$ Result = "";
// Four characters to be written to the image
For ($ I = 0; $ I <4; $ I ++)
{
Switch (rand (0, 1 ))
{
Case 0:
$ Result. = $ zhongwenku [rand (0, $ zhongwenku_size-1)];
Break;
Case 1:
$ Result. = dechex (rand (0, 15 ));
Break;
}

}
$ _ SESSION ["check"] = $ result;

// Create a true color image with a width of 100 and a height of 30
$ Img = imagecreatetruecolor (100, 30 );
// Assign the background color
$ Bg = imagecolorallocate ($ img, 0, 0 );
// Assign text color
$ Te = imagecolorallocate ($ img, 255,255,255 );
// Write a string on the image
// Imagestring ($ img, rand (), $ result, $ te );
// Write a special font based on the loaded font on the image
Imagettftext ($ img, 13, rand (2, 9), 20, 20, $ te, "MSYH. TTF", $ result );
$ _ SESSION ["check"] = $ result;
For ($ I = 0; $ I <3; $ I ++)
{
// $ T = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 ));
// Draw a line
Imageline ($ img, 0, rand (0, 20), rand (70,100), rand (0, 20), $ te );
}
$ T = imagecolorallocate ($ img, rand (0,255), rand (0,255), rand (0,255 ));
// Add noise to the image
For ($ I = 0; I I <200; $ I ++)
{
Imagesetpixel ($ img, rand (1,100), rand (1, 30), $ t );
}
// Send the http header to specify that the sent image is jpeg.
Header ("Content-type: image/jpeg ");
// Output jpeg image to the browser
Imagejpeg ($ img );
?>


Submit. php

The code is as follows:


Session_start ();
If (@ $ _ POST ['check'])
{
If ($ _ POST ["check"] === _ SESSION ["check"])
{
Echo "Congratulations! The verification code is entered correctly! ";
} Else {
Echo "Sorry, incorrect verification code input ";
}
}
?>


In the code, MRYH. ttf is the font you want to set.

Watermark is similar to the following 1. use the GD library function to generate an image, and write the specified character imagecreatetruecolor on the image to create a true color image...

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.