PHP generates a simple example to create a verification code, and php verification code instance _ PHP Tutorial

Source: Internet
Author: User
Tags imagejpeg
PHP generates a simple example to create a verification code and a php verification code instance. PHP generates a simple example to create a verification code. after reading the php verification code instance, you will not hit me. you will not talk much about it or start it. (there are not many people talking about it) 1.0 first, let's take a look at the code phpheader (simple example of Content PHP generating a verification code, php verification code instance

You won't beat me after reading it. you don't have to talk much about it)

1.0 first read the code

<? Phpheader ("Content-Type: text/html; Charset = UTF-8"); // Set the encoding style header of the page ("Content-Type: image/jpeg "); // notify the browser to output jpeg images $ img = imagecreatetruecolor (150, 50); // Create a canvas and set the size of X axis y axis 50 $ bgcolor = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255); // assign the background color imagefill ($ img, 0, 0, $ 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 (Black by default [even if it is called a true color image]). to change the background color, use the fill color function imagefill ($ img, $ 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-8"); // Set the encoding style header of the page ("Content-Type: image/jpeg "); // notify the browser to output jpeg images $ img = imagecreatetruecolor (150, 50); // Create a canvas and set the size of X axis y axis 50 $ bgcolor = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255); // assign the background color // add the interference line and cycle it three times, random background color for ($ I = 0; $ I <3; $ I ++) {$ linecolor = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255 ), mt_rand (0,255); imageline ($ Img, mt_rand (0,150), mt_rand (0,150), mt_rand (), mt_rand (), $ linecolor);} // add interference points and loop for 25 times, random background color for ($ I = 0; $ I <25; $ I ++) {$ dotcolor = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255 ), mt_rand (0,255); imagesetpixel ($ img, mt_rand (0,150), mt_rand (), $ dotcolor);} imagefill ($ img, 0, 0, $ bgcolor ); //// fill the background with the image imagejpeg ($ img); // output the 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 () in the image, use color in the x and y coordinates (the upper left corner of the image is 0, 0) draw a vertex.

Imagesetpixel ($ img, mt_rand (0,150), mt_rand (), $ dotcolor:

3.0 add verification letters and numbers

<? Phpheader ("Content-Type: text/html; Charset = UTF-8"); // Set the encoding style header of the page ("Content-Type: image/jpeg "); // notify the browser to output jpeg images $ img = imagecreatetruecolor (150, 50); // Create a canvas and set the size of X axis y axis 50 $ bgcolor = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255); // assign the background color // add the interference line and cycle it three times, random background color for ($ I = 0; $ I <3; $ I ++) {$ linecolor = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255 ), mt_rand (0,255); imageline ($ Img, mt_rand (0,150), mt_rand (0,150), mt_rand (), mt_rand (), $ linecolor);} // add interference points and loop for 25 times, random background color for ($ I = 0; $ I <25; $ I ++) {$ dotcolor = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255 ), mt_rand (0,255); imagesetpixel ($ img, mt_rand (0,150), mt_rand (), $ dotcolor );} // add letters or numbers to be verified $ rand_str = "qwertyuiopasdfghjklzxcvbnm1234567890"; // The letters and numbers that need to be verified $ str_arr = array (); // name an array for ($ I = 0; $ I <4; $ I ++) {// four cycles, that is, there are four random letters or numbers $ pos = mt_rand (0, strlen ($ rand_str)-1 ); $ str_arr [] = $ rand_str [$ pos]; // temporary switching} $ x_start = 150/4; // A single character's X axis position foreach ($ str_arr as $ key) {$ fontcolor = imagecolorallocate ($ img, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255); imagettftext ($ img, 25, mt_rand (-15,15 ), $ x_start, 50/2, $ fontcolor, "C:/Windows/Fonts/Verdana. TTF ", $ key); $ x_start + = 20; // after traversing, a single character along the x axis + 20} imagefill ($ img, 0, 0, $ 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:

It looks pretty cute.

The simple example of the above PHP code generation and production is all the content shared by the editor. I hope you can give us a reference and support the help house.

The hacker won't hit me after reading it. if you don't talk much about it or start it (there are not many people talking about it) 1.0 first, let's look at the code phpheader ("Content...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.