PHP generates the verification code, and php generates the verification code _ PHP Tutorial

Source: Internet
Author: User
Tags imagejpeg
PHP generates the verification code, and php generates the verification code. PHP generates and creates a verification code. php generates the verification code after reading it. if you don't hit me, you don't need to talk much about it. (there aren't many people talking about it.) 1.0 first, let's look at Code 1 first? Php2header (Content-Type: t PHP to generate a verification code, php to generate a verification code

You won't beat me after reading it. you don't have to talk much 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 )

Use imageline ()colorColor in imageimageFrom coordinatesx1,y1Tox2,y2(0 in the upper left corner of the image) draw a line segment.

Imageline ($ img, mt_rand (0,150), mt_rand (0,150), mt_rand (), mt_rand (), $ linecolor );

In this example, the coordinates of the canvas $ img arex1,y1Tox2,y2Random


Imagesetpixel ();

Imagesetpixel-draw a single pixel

1 bool imagesetpixel ( resource $image , int $x , int $y , int $color )

Imagesetpixel () inimageUsed in imagescolorColor inx,yDraw a point on the coordinate (0, 0 in the upper left corner of 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.

The hacker will be able to beat 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 Code 1 first? Php 2 header ("Content-Type: t...

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.