This article describes how to use php to create a dynamic random Verification Code. For more information, see
This article describes how to use php to create a dynamic random Verification Code. For more information, see
CAPTCHA is short for "Completely automatic Public Turing test to tell Computers and Humans Apart" (a Completely Automated Turing test that distinguishes Computers from Humans, it is a fully-automated public program that distinguishes a user from a computer or a person. It can prevent malicious password cracking, ticket flushing, and Forum bumping, effectively preventing a hacker from continuously logging on to a specific registered user using brute force cracking methods of specific programs, in fact, the verification code is now used by many websites. We have implemented this function in a relatively simple way.
This question can be generated and judged by computers, but it must be answered by humans. Because the computer cannot answer CAPTCHA's questions, users who answer questions can be considered as humans.
Php makes a dynamic verification code based on php image processing. The following describes the php image processing.
I. Introduction to php Image Processing
In PHP5, processing of dynamic images is much easier than before. PHP5 contains the GD extension package in the php. ini file. You only need to remove the corresponding comments of the GD extension package to use it normally. The GD library in PHP5 is the upgraded GD2 library, which contains some useful JPG functions that support true color image processing.
Generally, generated images are stored in the PHP document format, but dynamic images can be directly obtained through the HTML image Insertion Method SRC. For example, verification code, watermark, and thumbnail.
General process for creating images:
1) set the header to tell the browser the MIME type you want to generate.
2) create an image region based on which operations will be performed in the future.
3) Fill the background in the blank image area.
4) Draw the input text of the graphic outline on the background.
5). Output the final image.
6) Clear all resources.
7). Call images on other pages.
Step 1: Set the file MIME type, and change the output type to image stream.
The Code is as follows:
Header ('content-Type: image/png ;');
Generally, the generated image can be png, jpeg, gif, or wbmp.
Step 2: Create a graphic area and image background
Imagecreatetruecolor () returns an image identifier, representing a black image of x_size and y_size. Syntax: resource imagecreatetruecolor (int $ width, int $ height)
The Code is as follows:
$ Im = imagecreatetruecolor (200,200 );
Step 3: Draw a filling background in the blank image area
Color filler; imagecolorallocate -- assign color to an image; Syntax: int imagecolorallocate (resource $ image, int $ red, int $ green, int $ blue)
The Code is as follows:
$ Blue = imagecolorallocate (USD im, 0,102,255 );
Fill the blue color to the background; imagefill -- fill the area; Syntax: bool imagefill (resource $ image, int $ x, int $ y, int $ color)
The Code is as follows:
Imagefill ($ im, 0, 0, $ blue );
Step 4: Enter some lines and text on the blue background
Color Filler
The Code is as follows:
$ White = imagecolorallocate ($ im, 255,255,255 );
Draw two line segments: imageline
Imageline () uses color to draw a line segment from the coordinates x1, y1 to x2, y2 (0 in the upper left corner of the image. Syntax: bool imageline (resource $ image, int $ x1, int $ y1, int $ x2, int $ y2, int $ color)
The Code is as follows:
Imageline ($ im, 0, 0, 200,200, $ white );
Imageline ($ im, 200,0, 0,200, $ white );
Draw a line of string horizontally: imagestring
Imagestring () uses the col color to draw string s to the x and y coordinates of the image (this is the coordinate of the upper left corner of the string, and the upper left corner of the entire image is 0, 0 ). If the font is 1, 2, 3, 4, or 5, the built-in font is used. Syntax: bool imagestring (resource $ image, int $ font, int $ x, int $ y, string $ s, int $ col)
The Code is as follows:
Imagestring ($ im, 5, 66, 20, 'jingwhale ', $ white );
Step 5: output the final image
Imagepng () outputs the GD image stream (image) in PNG format to the standard output (usually in a browser), or outputs the file name to the file if filename is used. Syntax: bool imagepng (resource $ image [, string $ filename])
The Code is as follows:
Imagepng ($ im );
Step 6: I want to clear all the resources
Imagedestroy () releases the memory associated with the image. Syntax: bool imagedestroy (resource $ image)
The Code is as follows:
Imagedestroy ($ im );
Images Created by calling other pages (html)
The Code is as follows:
The sample code is as follows:
The Code is as follows:
<? Php
// Step 1: Set the object MIME type
Header ('content-Type: image/png ;');
// Step 2: Create a graphic area with an image background
$ Im = imagecreatetruecolor (200,200 );
// Step 3: Draw the filling background in the blank image area
$ Blue = imagecolorallocate (USD im, 0,102,255 );
Imagefill ($ im, 0, 0, $ blue );
// Step 4: Enter some lines and text on the blue background
$ White = imagecolorallocate ($ im, 255,255,255 );
Imageline ($ im, 0, 0, 200,200, $ white );
Imageline ($ im, 200,0, 0,200, $ white );
Imagestring ($ im, 5, 66, 20, 'jing. Whale ', $ white );
// Step 5: output the final image
Imagepng ($ im );
// Step 6: clear all the resources
Imagedestroy ($ im );
?>
Display Effect:
2. Create a dynamic verification code
Appendix: code source address https://github.com/cnblogs-/php-captcha
1. Create an image with a verification code and blur the background.
The random code is in hexadecimal notation. blur the background to add lines and snow to the image background.
1) create a random code
The Code is as follows: