PHP Verification Code Small Project:
<?php
Header (' Content-type:image/jpeg ');
$img =imagecreatetruecolor (120,40);//Build image
$element =array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ');//Captcha random letter, set array
$string = ";//define a variable that can set the captcha random letter
for ($i =0; $i <5; $i + +) {
$string. = $element [Rand (0,count ($element)-1)];
}//implement CAPTCHA random letter, put $element the array loop 5 times, generate 5 random letters
$color 1=imagecolorallocate ($img, Rand (200,250), Rand (200,250), Rand (200,250));//randomly assign colors to new images
$color 2=imagecolorallocate ($img, Rand (150,200), Rand (150,200), Rand (150,200));//randomly assign colors to new image pixel points
Imagefill ($img, 0,0, $color 1);//image coordinates and colors
for ($i =0; $i <100; $i + +) {
Imagesetpixel ($img, Rand (0,120), Rand (0,40), $color 2);
}//a single pixel loop 100 times, generating 100 pixels at random coordinates on the page.
for ($i =0; $i <3; $i + +) {
Imageline ($img, Rand (0,120/2), Rand (0,40), Rand (60,120), Rand (0,40), $color 2);
}//line Loops 3 times, generates 3 lines in random coordinates on the page
Imagettftext ($img, 25,rand ( -5,5), Rand (5,15), Rand (30,35), $color 2, ' Font/fudgie.ttf ', $string);
Write Captcha font, set size, angle, orientation, color
Imagejpeg ($img);//output format JPEG image
Imagedestroy ($img);//Release image resources
?>
Step 1: use Imagecreatetruecolor to build the image and use Imagefill to locate and assign colors to the image. Step 2: Randomly assign colors to the newly created image using Imagecolorallocate to load variable $color1, then put $color1 back into Imagefill this function inside. Step 3: Randomly assign colors to the newly created image using Imagecolorallocate to load variable $color2, then put $color2 back into Imagesetpixel This function, use Imagesetpixel to set the pixel points on the verification code and use the For loop, Loop 100 times on the verification code, generating 100 random coordinate pixel points. Step 4: use Imageline to set the line on the verification code and use the For loop to loop 3 times on the validation, resulting in 3 random coordinates of the line, Line color in order to quickly and do not use Imagecolorallocate to create a 3rd color, followed by $color2, real international projects, there is a need to create color. Step 5: Use Imagettftext to create the font on the verification code, the function can enter other styles of fonts, specific fonts can be downloaded on the net , after downloading, Put the font TTF file in the same directory as the currently written PHP file, and then set the path to the TTF file on the imagettftext , see the PHP manual for details, The next step is that I think the difficulty is to make the verification code on the letter randomly converted. Step 6: To let the code on the letter random conversion, the first to set a variable $string, which writes an array of arrays, the array of contains you want to turnA random letter, such as the one I made above, $element, an array inside, containing the letters in the a-k , and then using a For loop for loops 5 times. , the circular a-k is randomly 5 times inside the alphabet. There are two note points, the first is that before the loop, you need to set the variable $string, and $string because the variable needs to be preceded by the loop redefine, I was in this for a long time, because the first two for loop and there is no variable inside, so lead me here forget to redefine the variable, the second note is rand (0,count ($element)-1), why $element subtract 1 &NBSP, and then asked the gods to know the count this function is the number of statistical arrays, here the number of statistics array a-k is 11, but Rand is a random array subscript, is calculated from the beginning of program 0, A-k if a from 0 to K only to 10, and the function $ The element count is 11, so $element minus 1 becomes 10 so rand can calculate it. Step 7: Output The format when the image is created, I am here jpeg and release the picture resource.
The above steps welcome the major gods to add, Exchange, there is also very normal writing, I am a rookie, but can write on the blog, are testing the effect of the following, we can look at, welcome you to exchange.
Finally, there is a question point, not important, so did not ask, but if there is a great God, or hope the great God, Daniel can teach:
About the project I wrote $string.= $element [rand (0,count ($element)-1)], the variable $string to be used as a connector. To connect to a random 5 letters, I try not to add a connector, but only a random letter, so I think, should be $ Element[rand (0,count ($element)-1)] This program executes, only a random letter, and then return to the variable $string, plus the connector and then continue to run, until the loop 5 ends, do not know if this understanding is correct, if there is a great God to see, Trouble to tell the logic in detail, younger brother grateful.
This article is from the "Technology Heaven" blog, so be sure to keep this source http://jonhson.blog.51cto.com/10559861/1684174
Learning Experience (1): How to use PHP to write verification code daily a more self-supervision