PHP5 in GD Library generate graphics verification code (with Chinese characters) _php tutorial

Source: Internet
Author: User
Tags imagejpeg
Using GD library in PHP5 to generate graphics verification code



This is similar to the following
1. Create a picture with the GD library function and write the specified character on the image
Imagecreatetruecolor Create a new true color image
Imagecolorallocate Assigning color (palette) to an image
Imagestring drawing characters
Imageline Drawing Lines
Imagesetpixel hit pixel points
2. Output image
Imagejpeg ($IMG);
PHP implementation process, code comments in detail, here do not do too much explanation
verify.php
Copy CodeThe code is as follows:
1.qi Enable GD Library GD library provides a series of APIs for working with images, using the GD library to process pictures, or to create images.
The GD library on the site is often used to generate thumbnails or to add watermarks to images or generate reports on site data.
Session_Start ();
The GBK encoded string into the UTF-8 string, the first argument is written GBK because the php file stored in the host encoding is GBK encoded
UTF-8 encoding Browser Universal support, strong versatility, this translates into UTF-8
$str = Iconv ("GBK", "Utf-8", "The mortal life of the Green Mountains and monuments open heart will clouds unroll happiness will always accompany you");
if (!is_string ($str) | |!mb_check_encoding ($STR, "Utf-8"))
{
Exit ("Not a string or not a utf-8");
}
$zhongwenku _size;
To get the length of a string by UTF-8 encoding
$zhongwenku _size = Mb_strlen ($str, "UTF-8");
Import the above 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 write on a picture
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 picture width 100, Height 30
$img = Imagecreatetruecolor (100, 30);
Assigning background colors
$BG = imagecolorallocate ($img, 0, 0, 0);
Assigning text colors
$te = Imagecolorallocate ($img, 255,255,255);
Write a string on a picture
Imagestring ($img, Rand (3,8), Rand (1,70), Rand (1,10), $result, $te);
A special font can be written on a picture based on the loaded font
Imagettftext ($IMG, Max, Rand (2, 9), $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 Line
Imageline ($img, 0, rand (0,), rand (70,100), rand (0,), $te);
}
$t = Imagecolorallocate ($img, rand (0, 255), rand (0, 255), rand (0, 255));
Add noise to a picture
for ($i =0; $i <200; $i + +)
{
Imagesetpixel ($img, rand (1, +), rand (1,), $t);
}
Send HTTP header information specifies that the JPEG in image is sent this time
Header ("Content-type:image/jpeg");
Output JPEG image to browser
Imagejpeg ($IMG);
?>

submit.php
Copy CodeThe code is as follows:
Session_Start ();
if (@$_post[' check ')
{
if ($_post["check"] = = $_session["Check"])
{
echo "Congratulations! Verify that the code is entered correctly! ";
}else{
echo "Sorry to verify code input error";
}
}
?>


The Mryh.ttf in your code is the font you want to set.

http://www.bkjia.com/PHPjc/328100.html www.bkjia.com true http://www.bkjia.com/PHPjc/328100.html techarticle using the GD library in PHP5 to generate a graphical verification code similar to the following 1. Use the GD library function to generate a picture and write the specified character on the picture Imagecreatetruecolor create a new 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.