Another Chinese Valentine's Day! The basic functions of this program were implemented last night and completed at noon today. The only pity is that the alarm did not wake me up, so there was no meteor shower! Use this code for compensation! The first time I wrote a technical blog, I could give it a review later, and I 'd like to take a look at my own ideas. Don't give me any further information! Pai_^ Test
Another Chinese Valentine's Day! The basic functions of this program were implemented last night and completed at noon today. The only pity is that the alarm did not wake me up, so there was no meteor shower! Use this code for compensation! The first time I wrote a technical blog, I could give it a review later, and I 'd like to take a look at my own ideas. Don't give me any further information! Pai_^ Test
Another Chinese Valentine's Day! The basic functions of this program were implemented last night and completed at noon today. The only pity is that the alarm did not wake me up, so there was no meteor shower! Use this code for compensation! The first time I wrote a technical blog, I could give it a review later, and I 'd like to take a look at my own ideas. Don't give me any further information! Pai_^
Verification Code features:
1) random display of Characters
2) determine the verification code type as you like (0-show only numbers, 1-show lower-case letters and numbers, 2-show numbers and upper-case letters)
3) You can set the display size as required.
4) You can add interference lines and interference points.
Steps
Step 1: implement the getCode () function to randomly generate a verification code with a specified number of characters ()
/*** Function: randomly generate a verification code function * @ param $ code_len the number of characters displayed in the Verification Code * @ param $ type verification code type (default 0-all are numbers, 1-lowercase letters and numbers, 2-lowercase letters, uppercase letters, and numbers) * @ author MaChaoHui
**/Function getCode ($ code_len, $ type = 2) {$ str = 'hangzhou'; $ type_pos = array (9, 35, strlen ($ str)-1 ); // type location demarcation // the content of the randomly generated Verification Code $ content = ''; for ($ I = 0; $ I <$ code_len; $ I ++) {$ content. = $ str [rand (0, $ type_pos [$ type])];} return $ content ;}
The effect in the browser is:
Step 2: Draw the verification code background.
We know that there are several steps for drawing the GD library in PHP: 1) drawing a canvas, assigning background color; 2) drawing; 3) output image; 4) destroying the canvas (releasing memory ). So the next step is to follow these steps to draw the verification code.
1) Draw the canvas and assign the background color (imagecreatetruecolor ($ width, $ height); function)
$ Width = $ code_len * 20; $ height = 30; $ im = imagecreatetruecolor ($ width, $ height); $ bg = imagecolorallocate ($ im, 200,200,200); // 2) draw background imagefill ($ im, 0, 0, $ bg); // 3 ). output image header ("Content-Type: image/png"); // sets the response header information imagepng ($ im); // 4 ). destroy canvas (release memory) imagedestroy ($ im );
The result is as follows:
Step 3: Draw the font of the verification code.
Core code:
// Draw the verification code content (draw by character) for ($ I = 0; $ I <$ code_len; $ I ++) {imagettftext ($ im, 18, rand (-40, 40), 6 + (18 * $ I), 24, $ color [rand (0, 4)], 'ariblk. ttf', $ str [$ I]);}
The effect is as follows:
It can be seen that there are no interference lines and interference points, and there is no border. It is not very nice. So add the following.
Step 4: Add interference lines, interference points, and borders.
Core code:
// Draw the border imagerectangle ($ im, 0, 0, $ width-1, $ height-1, $ color [rand (0, 4)]); // randomly add interference points for ($ I = 0; $ I <200; $ I ++) {$ col = imagecolorallocate ($ im, rand (0,255 ), rand (0,255), rand (0,255); imagesetpixel ($ im, rand (0, $ width), rand (0, $ height), $ col );} // randomly add interference lines for ($ I = 0; $ I <5; $ I ++) {$ col = imagecolorallocate ($ im, rand (0,255 ), rand (0,255), rand (0,255); // draw random color imageline ($ im, rand (0, $ width), rand (0, $ height ), rand (0, $ width), rand (0, $ height), $ col );}
Usage:
Effect: