PHP generate verification code

Source: Internet
Author: User
PHP generates a production verification code and you will not hit me after reading it. you will not talk much or start it. (there are not many people talking about it)

1.0 first read the code

1
 

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? )

1 resource imagecreatetruecolor ( int $width , int $height )

Both imagecreatetruecolor () and imagecreate () functions can create canvases.

1 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

1 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

1 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

1
 

Function analysis:

①Imageline ();

Imageline-draw a line segment

1 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 random from the coordinates x1, y1 to x2, y2.


②Imagesetpixel ();

Imagesetpixel-draw a single pixel

1 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 );
The specific meaning is the same as above.

:

3.0 add verification letters and numbers

1
 

Function:

Imagettftext ();

Imagettftext-use the TrueType font to write text to the image

1 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.

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.