PHP5 picture CAPTCHA Implementation Code _php tutorial

Source: Internet
Author: User
Tags imagejpeg
Functions of the GD library
1,imagecreatetruecolor-----Create a true-color image
Imagecreatetruecolor (int x_size,int y_size)//x is wide, y represents high
2,imagecolorallocate Assigning color (palette) to an image
Imagecolorallocate (Resource Image,int red,int green,int blue)//red,green,blue----three primary colors
3,imagestring Drawing functions
Iamgestring (Resource image,font,int x,int y, content, color);
4, Output function
The header of PHP is defined as the action of the Head, which supports the 3 type in PHP5:
1,content-type:xxxx/yyyy
2,location:xxxx:yyyy/zzzz
3,status:nnn xxxxxx
XXXX/YYYY indicates the type of content file
such as: Image/gif
Image/jpeg
Image/png
Example: Header ("Content-type:image/jpeg")
There is a corresponding image type in the GD library
Imagejpeg (), Imagegif (), Imagepang ()
5,imageline Drawing Line function
Iamgeline (Resource image,int x1,int y1,int x2,int y2,int color);
Image---Pictures
X1---start coordinates
Y1
X2---endpoint 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 How to insert a CAPTCHA code into Chinese
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 the number
2, Dechex (decimal)//convert to hexadecimal
Steps to do the verification code:
Generate random number--Create picture--random number as picture--Save in session
Enter code example
gdchek.php
Copy CodeThe code is as follows:
/*
* 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");
Validation of adding Chinese
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);
?>

login.php
Copy CodeThe code is as follows:
/*
*
*
*/
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";
}
}
?>

http://www.bkjia.com/PHPjc/320947.html www.bkjia.com true http://www.bkjia.com/PHPjc/320947.html techarticle the function of the GD library 1,imagecreatetruecolor-----Create a true-color image imagecreatetruecolor (int x_size,int y_size)//x represents a wide, y represents a high 2,imagecolorallocate for a piece of 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.