1 first read the code first
<?php
Header ("content-type:text/html; charset=utf-")//sets the encoding style
header (" Content-type:image/jpeg ") of the page;//notifies the browser that the image is in JPEG format
$img = Imagecreatetruecolor (,);//create canvas and set size x axis y axis
$bgcolor = Imagecolorallocate ($img, Mt_rand (,), Mt_rand (,), Mt_rand (,)) ;//Assign Background color
Imagefill ($img,,, $bgcolor);////fill the background to the image
imagejpeg ($img);//Output Image
Imagedestroy ($img);//Destroy image
OK, now combine the above code to analyze and analyze several functions used above:
①imagecreatetruecolor ();
imagecreatetruecolor-new A true color image (feel wow, so long, in fact, a very good look at the Image/create/true/color, what is true color image? Look down)
Resource Imagecreatetruecolor (int $width, int $height)
Imagecreatetruecolor () and imagecreate () two functions can create a canvas
Imagecreatetruecolor () creates a black image of X and Y (the default is black [even if the name is True Color image]), and you want to change the background color
To use the Fill color function Imagefill ($img, 0,0, $color);
Imagecreate Create a new blank image resource and add a background color with imagecolorallocate ()
The top two functions are just two ways to function
②imagecolorallocate ();
imagecolorallocate-assign color to an image
int Imagecolorallocate (resource $image, int $red, int $green, int $blue)
The colors are combined in red, green and blue, which are 0 to 255 integers or hexadecimal 0x00 to 0xFF.
③mt_rand ();
mt_rand-to generate better random numbers
int Mt_rand (int $min, int $max)
$min optional, returned minimum value (default: 0) $max optional, maximum return (default: Mt_getrandmax ())
This is used to let him randomly generate background color, 0-255 random value. So the page did not refresh once the canvas background color is different. Effect Chart:
2.0 start to the inside to do disturbing lines, disturbances points. Prevent validating images from being recognized by seconds
<?php
Header ("content-type:text/html; charset=utf-")//sets the encoding style
header (" Content-type:image/jpeg ") of the page;//notifies the browser that the image is in JPEG format
$img = Imagecreatetruecolor (,);//create canvas and set size x axis y axis
$bgcolor = Imagecolorallocate ($img, Mt_rand (,), Mt_rand (,), Mt_rand (,)) ///Assign background color
//Add interference line, and loop, background color randomly for
($i =; $i <; $i + +) {
$linecolor = imagecolorallocate ($img, Mt_rand (,), Mt_rand (,), Mt_rand (,));
Imageline ($img, Mt_rand (,), Mt_rand (,), Mt_rand (,), Mt_rand (,), $linecolor);
}
Add interference points, and cycle times, background color random for
($i =; $i <; $i + +) {
$dotcolor = imagecolorallocate ($img, Mt_rand (,), Mt_rand (,), MT _rand (,));
Imagesetpixel ($img, Mt_rand (,), Mt_rand (,), $dotcolor);
}
Imagefill ($img,,, $bgcolor); Fill the background to the image
imagejpeg ($img);//Output Image
Imagedestroy ($img);//Destroy image
Function Analysis:
①imageline ();
imageline-Draw a line segment
Imageline () Draws a line segment from the coordinate x1,y1 to the x2,y2 (0, 0) in image images in color color.
Imageline ($img, Mt_rand (0,150), Mt_rand (0,50), Mt_rand (0,150), Mt_rand (0,50), $linecolor); This means the canvas $img from coordinates x1,y1 to x2, Y2 random
②imagesetpixel ();
imagesetpixel-draw a single pixel
BOOL Imagesetpixel (resource $image, int $x, int $y, int $color)
Imagesetpixel () Draws a point on the image image in the X,y coordinate (0,0 in the upper-left corner of the image) in color.
Effect Chart:
3.0 Add validated Alphanumeric
<?php header ("content-type:text/html; charset=utf-");//Set the encoding style header (" Content-type:image/jpeg ") of the page;//notification browser output is JPEG image $img = Imagecreatetruecolor (,);
/Create the canvas and set the size x axis y axis $bgcolor = imagecolorallocate ($img, Mt_rand (,), Mt_rand (,), Mt_rand (,));//Assign background color//Add interference line, and cycle times, background color random for ($i =; $i <; $i + +) {$linecolor = Imagecolorallocate ($img, Mt_rand (,), Mt_rand (,), Mt_rand (,)); Imageline ($img, Mt_
Rand (,), Mt_rand (,), Mt_rand (,), Mt_rand (,), $linecolor);
//Add interference points, and cycle times, background color randomly for ($i =; $i <; $i + +) {$dotcolor = Imagecolorallocate ($img, Mt_rand (,), Mt_rand (,), Mt_rand (,));
Imagesetpixel ($img, Mt_rand (,), Mt_rand (,), $dotcolor); //Add the letters or numbers that need to be validated $rand _str = "qwertyuiopasdfghjklzxcvbnm";//need to use some letters and numbers to verify $STR _arr = Array (); Name an array for ($i =; $i <; $i + +) {//cycle times, there are four random letters or numbers $pos = Mt_rand (, strlen ($rand _str)-), $str _arr[] = $rand _str[$pos]; Temporary swap} $x _start=/;//a single character x axis position foreach ($str _arr as $key) {$fontcolor = Imagecolorallocate ($img, Mt_rand (,), Mt_rand (,),
Mt_rand (,)); Imagettftext ($img, Mt_ranD (-,), $x _start,/, $fontcolor, "C:/windows/fonts/verdana.ttf", $key); $x _start +=;//traverse the single character along the x-axis +} imagefill ($img,,, $bgcolor); Fill the background to the image imagejpeg ($img); Output Image Imagedestroy ($img); Destroying Images?>
Function:
Imagettftext ();
Imagettftext-writes text to an image with a TrueType font
Analyze the following code:
$img-----------Canvas
-----------the size of the font.
Mt_rand ( -15,15)----------angle, 0 degrees is read from left to right text. A higher number indicates a counter-clockwise rotation. For example, 90 degrees represents text that is read from the bottom up. (is the font angle of the problem,)
$x _start----------Easy to understand is the x-axis position of the character
50/2 the height of the----------character
$fontcolor----------Character Color
Font style path for "C:/windows/fonts/verdana.ttf"----------characters
$key-----------The characters after the traversal
Effect:
The above content is this article to everybody introduced the newest most entire PHP produces the verification Code code detailed explanation (recommendation) The entire narration, hoped that has the help to everybody!