Php image verification code

Source: Internet
Author: User
Tags manual explode function prototype image identifier rand

Php image verification code

<? Php
// FileName: authimg. php
// Description:
// Creater: alvar
// Createtime: 2006-5-4
// Lastmodtime:
Session_start ();
?>
<? Php
// Generate a verification code Image
Header ("Content-type: image/PNG ");
Srand (double) microtime () * 1000000); // play the next seed that generates random numbers to facilitate the use of the following random number generation
// Session_start (); // Save the random number to the session.
$ _ SESSION ['authnum'] = "";
$ Im = imagecreate (90,20) or die ("Cant's initialize new GD image stream! "); // Specifies the image background size.
$ Red = ImageColorAllocate ($ im, 255, 0, 0); // you can specify three colors.
$ White = ImageColorAllocate ($ im, 255,255,255 );
$ Gray = ImageColorAllocate ($ im, 200,200,200 );
// Imagefill ($ im, $ gray); // use the area filling method, set (0, 0)
Imagefill ($ im, 0, 0, $ white); // ed
// Generate a verification code mixed with numbers and letters
$ Ychar = ", A, B, C, D, E, F, G, H, I, J, K, L, M, n, O, P, Q, R, S, T, U, V, W, X, Y, Z ";
$ List = explode (",", $ ychar );
For ($ I = 0; $ I <4; $ I ++ ){
$ Randnum = rand (0, 35 );
$ Authnum. = $ list [$ randnum]. ""; // add a space to the ed.
}
// While ($ authnum = rand () % 100000) <10000); // generates a random four-digit number.
// Print the four-digit integer verification code into the image
$ _ SESSION ['authnum'] = $ authnum;
// Int imagestring (resource image, int font, int x, int y, string s, int col)
Imagestring ($ im, 5, 10, 3, $ authnum, $ red );
// Use the col color to draw the string s to the x and y coordinates of the image (0, 0 in the upper left corner of the image ).
// If the font is 1, 2, 3, 4, or 5, use the built-in font.

For ($ I = 0; $ I <400; $ I ++) {// add interference pixels {
$ Randcolor = ImageColorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
// Imagesetpixel ($ im, rand () % 90, rand () % 30, $ randcolor );
Imagesetpixel ($ im, rand () % 90, rand () % 30, $ gray );
}
ImagePNG ($ im );
ImageDestroy ($ im );
?>

Recently, I am going to work on a school project and use image verification technology to ensure security. So I am looking for an article,
After a while, write down your own experiences,
Please correct me when I understand the mistake. I am also a dish,
I have written a good article for analysis, or I can view his code based on my own ideas.

The key to php image verification is using a function.
Imagestring () is a function. It is helpful to understand the function prototype.
// Int imagestring (resource image, int font, int x, int y, string s, int col );
Reading the manual will be helpful.
Imagestring () uses the col color to draw string s to the x and y coordinates of the image (0 and 0 in the upper left corner of the image ). If the font is 1, 2, 3, 4, or 5, the built-in font is used.
There are three key parameters.
Resource image, string s, int col
1,
Next let's take a look
Resource image, int col
Why are these two analyses analyzed together? Let's take a look.
This image is a drawn image.
To draw a chart, follow the steps below
1.1
The first is to create an image file stream.
Of course, imagecreateturecolor () can also be used,
Imagecreate is used as an example,
Refer to the function prototype and manual explanation for the old method.
Resource imagecreate (int x_size, int y_size)
Imagecreate () returns an image identifier, representing a blank image of x_size and y_size.
Then we can use it to create a blank image.
$ Im = imagecreate (90,30 );
1.2
Because it is blank, we need to find the fill color for it.
This function is used.
Imagecolorallocate ()
View prototype and manual
Int imagecolorallocate (resource image, int red, int green, int blue)
Imagecolorallocate () returns an identifier representing a color consisting of a given RGB component. The image parameter is the return value of the imagecreate () function. Red, green, and blue are the required colors respectively. These parameters are integers from 0 to 255 or hexadecimal values 0x00 to 0xFF. Imagecolorallocate () must be called to create the color of each image used in the image.
Let's take a look at the last sentence. imagecolorallocate () must be called to create the color of each image used in the image. Then we will create several colors to facilitate future filling, the color is also
There are three key parameters.
Resource image, string s, int col
Int col color settings
Let's set three colors.
$ Red = imagecolorallocate ($ im, 255, 0, 0 );
$ White = ImageColorAllocate ($ im, 255,255,255 );
$ Gray = ImageColorAllocate ($ im, 200,200,200 );
There is no need to explain these three, that is, the color of r g B.
1.3,
Since the blank image and fill color have come out, let's fill it.
This function is used.
Imagefill ()
Int imagefill (resource image, int x, int y, int color)
Imagefill () is filled with the color execution area (that is, with x, y points are in the same color and adjacent points are filled ).
Imagefill ($ im, $ white); // fill in white to make interferon more effective
In this case, two of my three parameters have been resolved, resource image, int col
Let's look at the second parameter string s;

2
The second parameter is string s.
2, 1
Because it is a verification code to prevent malicious attacks, it must be set to random to avoid problems,
Srand (double) microtime () * 1000000); // play the next seed that generates random numbers to facilitate the use of the following random number generation,
Generate a verification code that combines numbers and letters
$ Ychar = ", A, B, C, D, E, F, G, H, I, J, K, L, M, n, O, P, Q, R, S, T, U, V, W, X, Y, Z ";
$ List = explode (",", $ ychar); // The explode function is relatively simple, that is, the characters at each interval in advance and they form an array, if you don't understand it, read the manual. It's easy to understand.
An array containing the 36 characters has been generated, and the rest is to generate a random four-digit number.
For ($ I = 0; $ I <4; $ I ++ ){
$ Randnum = rand (0, 35 );
$ Authnum. = $ list [$ randnum]. ""; // add a space to the ed.
}
In this loop, the number of loops is four times. I am still explaining it. It is actually very simple,
For the first loop, rand () indicates that an array between 0 and 35 is randomly displayed.
Assume there are 0th, which is 1 $ authnum = 1 ."";
Second Cycle
If $ randum = 12, $ authnum = 1. $ list [$ randnum]. "";
That is, $ authnum = 1 C;
This loops to the fourth place,
Then a random four-digit number is generated,

Since we have solved all three parameters, we can use
The imagestring () function is used to create an image,
Imagestring ($ im, 5, 10, 3, $ authnum, $ red );
After the image is created, we will output the image.
Imagepng ($ im );
Imagedestroy ($ im );
At the same time, we need to explain in the file header the type of the file to be output
Header ("Content-type: image/PNG ");

3
In fact, the function is implemented, but for better security, we need to add interferon,
What is interferon? It's the interference factor.
This function is used.
Int imagesetpixel (resource image, int x, int y, int color)
Imagesetpixel () uses color in the image to draw a point on the x and y coordinates (0, 0 in the upper left corner of the image.
Let's look at his explanation and draw a point, so we must use a loop,
Just draw a few more points.
For ($ I = 0; $ I <400; $ I ++) {// add interference pixels
$ Randcolor = ImageColorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
Imagesetpixel ($ im, rand () % 90, rand () % 30, $ gray );
}
The first of the for loop generates random colors. Of course, we can also use
Here we use the $ gray defined above, which is the number of pixels produced by 400.
I have nothing to explain.

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.