PHP5 _php example of generating graphics verification code (with Chinese characters) in GD Library

Source: Internet
Author: User
Tags imagejpeg rand
Generating graphics verification code using GD library in PHP5



Similar to the following
1. Use the GD library function to generate pictures and write the specified characters on the picture
Imagecreatetruecolor Create a new true color image
Imagecolorallocate assigns colors to an image (color palette)
Imagestring drawing characters
Imageline Draw Lines
Imagesetpixel dozen pixels.
2. Output picture
Imagejpeg ($IMG);
PHP implementation process, code comments in detail, here do not explain too much
verify.php
Copy Code code as follows:

<?php
1.qi enabling the GD Library GD Library provides a series of APIs for working with pictures, using the GD library to work with pictures, or to generate pictures.
The GD library on the site is typically used to generate thumbnails or to add watermarks to images or to generate reports on Web site data.
Session_Start ();
Converts a GBK encoded string into a UTF-8 string, and the first argument is written GBK because the encoding that this PHP file stores in the host is GBK encoded
UTF-8-coded browsers are generally supported and versatile, and this translates into UTF-8
$str = Iconv ("GBK", "Utf-8", "all mortal beings green mountains and monuments open Heart will be clouds unroll happiness will always accompany you");
if (!is_string ($str) | | |!mb_check_encoding ($STR, "Utf-8"))
{
Exit ("Not a string or not utf-8");
}
$zhongwenku _size;
Gets the length of the string by UTF-8 encoding
$zhongwenku _size = Mb_strlen ($str, "UTF-8");
Import the above characters into an 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 the 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);
Assign background color
$BG = imagecolorallocate ($img, 0, 0, 0);
Assign text color
$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);
You can write special fonts on a picture based on the load font
Imagettftext ($img, 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 picture to browser
Imagejpeg ($IMG);
?>

submit.php
Copy Code code as follows:

<?php
Session_Start ();
if (@$_post[' check ')]
{
if ($_post["check"] = = $_session["Check")
{
Echo, Congratulations! Verify code input is correct! ";
}else{
echo "Excuse me for validating code input error";
}
}
?>
<form action= "" method= "POST" >
</br>
<input type= "text" name= "Check" ></br>
<input type= "Submit" value= "Submit" ></br>
</form>

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

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.