This article mainly introduces the latest and most complete PHP code generation and production verification code details (recommended) related information, very good reference value, friends who need to refer
1.0 first read the code
<? Phpheader ("Content-Type: text/html; Charset = UTF-"); // Set the encoding style header ("Content-Type: image/jpeg") of the page "); // notify the browser that the image is output in jpeg format $ img = imagecreatetruecolor (,); // Create a canvas and set the size x axis $ bgcolor = imagecolorallocate ($ img, mt_rand (,), mt_rand (,), mt_rand (,); // assign the background color imagefill ($ img, $ bgcolor ); //// fill the background with the image imagejpeg ($ img); // output the image imagedestroy ($ img); // destroy the image?>
Well, now we can use the above code to analyze the functions used above:
① Imagecreatetruecolor ();
Imagecreatetruecolor-create a true color image (wow, it's so long. In fact, take a closer look at the well-remembered image/create/true/color. what is a true color image? )
resource imagecreatetruecolor ( int $width , int $height )
Both imagecreatetruecolor () and imagecreate () functions can create canvases.
resource imagecreate ( int $x_size , int $y_size )
Imagecreatetruecolor () is a black image of the size of x and y (the default value is Black [even if it is called a true color image]). to change the background color, you must
Use the fill color function imagefill ($ img, 0, 0, $ color );
Imagecreate creates a blank image resource and adds the background color with imagecolorAllocate ().
The above two functions are just two methods of a function.
② Imagecolorallocate ();
Imagecolorallocate-assign color to an image
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
The color is a combination of red, green, and blue. These parameters are integers ranging from 0 to 255 or hexadecimal values 0x00 to 0xFF.
③ Mt_rand ();
Mt_rand-generate better random numbers
int mt_rand ( int $min , int $max )
$ Min (optional) minimum value returned (default: 0) $ max (optional) maximum value returned (default: mt_getrandmax ())
This is used to randomly generate the background color. The value is 0. Therefore, the background color of the canvas is different once the page is refreshed. :
Starting from 2.0, we made interference lines and interference points inside. Prevents verification images from being recognized in seconds
<? Phpheader ("Content-Type: text/html; Charset = UTF-"); // Set the encoding style header ("Content-Type: image/jpeg") of the page "); // notify the browser that the image is output in jpeg format $ img = imagecreatetruecolor (,); // Create a canvas and set the size x axis $ bgcolor = imagecolorallocate ($ img, mt_rand (,), mt_rand (,), mt_rand (,); // assign the background color // add the interference line and loop the times. The background color is random for ($ I =; $ I <; $ I ++) {$ linecolor = imagecolorallocate ($ img, mt_rand (,); imageline ($ img, mt_rand (,), mt_rand (,), mt_rand (, ), Mt_rand (,), $ linecolor) ;}// add interference points and loop times. The background color is random for ($ I =; I I <; $ I ++) {$ dotcolor = imagecolorallocate ($ img, mt_rand (,); imagesetpixel ($ img, mt_rand (,), mt_rand (,), $ dotcolor);} imagefill ($ img, $ bgcolor); // fill the background in the image imagejpeg ($ img ); // output image imagedestroy ($ img); // destroy the image?>
Function analysis:
① Imageline ();
Imageline-draw a line segment
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
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.
Imageline ($ img, mt_rand (0,150), mt_rand (0,150), mt_rand (), mt_rand (), $ linecolor); this indicates that the canvas $ img is from the coordinate 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 in the x and y coordinates (0, 0 in the upper left corner of the image) using color in the image.
Imagesetpixel ($ img, mt_rand (0,150), mt_rand (), $ dotcolor );
:
3.0 add verification letters and numbers
<? Phpheader ("Content-Type: text/html; Charset = UTF-"); // Set the encoding style header ("Content-Type: image/jpeg") of the page "); // notify the browser that the image is output in jpeg format $ img = imagecreatetruecolor (,); // Create a canvas and set the size x axis $ bgcolor = imagecolorallocate ($ img, mt_rand (,), mt_rand (,), mt_rand (,); // assign the background color // add the interference line and loop the times. The background color is random for ($ I =; $ I <; $ I ++) {$ linecolor = imagecolorallocate ($ img, mt_rand (,); imageline ($ img, mt_rand (,), mt_rand (,), mt_rand (, ), Mt_rand (,), $ linecolor) ;}// add interference points and loop times. The background color is random for ($ I =; I I <; $ I ++) {$ dotcolor = imagecolorallocate ($ img, mt_rand (,); imagesetpixel ($ img, mt_rand (,), mt_rand (,), $ dotcolor);} // add the letter or number to be verified $ rand_str = "qwertyuiopasdfghjklzxcvbnm"; // The letters and numbers that need to be verified $ str_arr = array (); // name an array for ($ I =; $ I <; $ I ++) {// loop times, that is, there are four random letters or numbers $ pos = mt_rand (, strlen ($ rand_str)-); $ str_arr [] = $ rand_str [$ pos ]; // Temporary switching} $ x_start = // a single character's 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 + =; // after traversing, a single character follows the x axis +} imagefill ($ img, $ bgcolor ); //// fill the background with the image imagejpeg ($ img); // output the image imagedestroy ($ img); // destroy the image?>
Function:
Imagettftext ();
Imagettftext-use the TrueType font to write text to the image
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
Analyze the following code:
imagettftext($img, 25, mt_rand(-15,15), $x_start, 50/2, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key);
$ Img ----------- canvas
25 ----------- font size.
Mt_rand (-15, 15) ---------- angle representation of the angle, 0 degrees is the text read from left to right. A higher value indicates a counter-clockwise rotation. For example, 90 degrees indicates the text read from the bottom up. (It is about the font angle ,)
$ X_start ---------- easy to understand is the x-axis position of a character.
50/2 ---------- character height
$ Fontcolor ---------- character color
"C:/Windows/Fonts/Verdana. TTF" ---------- character font style path
$ Key ----------- characters after traversal
Effect:
The above is a full description of the latest and most complete PHP code generation and production verification code (recommended) introduced in this article. I hope it will help you!