Get the App
SLTechnology News&Howtos  ›  Development  › 

How to generate CAPTCHA with PHP

Shulou Source: shulou.com Published: 2022-06-03 19:05:49 11月12日 Update

This article mainly introduces "how to use PHP to generate CAPTCHA". In daily operation, I believe many people have doubts about how to use PHP to generate CAPTCHA. The editor consulted all kinds of data and sorted out a simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "how to use PHP to generate CAPTCHA". Next, please follow the editor to study!

1.0 look at the code first

OK, now combined with the above code, let's analyze and analyze the functions used above:

① imagecreatetruecolor ()

Imagecreatetruecolor-create a new true color image (wow, it's so long, it's actually easy to remember on a closer look, image/create/true/color, what is a true color image? Look down)

Resource imagecreatetruecolor (int $width, int $height)

Both the imagecreatetruecolor () and imagecreate () functions can create a canvas

Resource imagecreate (int $x_size, int $y_size)

Imagecreatetruecolor () creates a black image of size x and y (default is black [even if it is called a true color image]). If you want to change the background color, you need to use the fill color function imagefill ($img,0,0,$color).

Imagecreate create a new blank image resource and add background color with imagecolorAllocate ()

The above two functions are just two methods of one function.

② imagecolorallocate ()

Imagecolorallocate-assign a color to an image

Int imagecolorallocate (resource $image, int $red, int $green, int $blue)

The colors are combined with red, green and blue, and these parameters are integers from 0 to 255 or hexadecimal 0x00 to 0xFF.

③ mt_rand ()

Mt_rand-generate better random numbers

Int mt_rand (int $min, int $max)

Minimum returned value of $min optional (default: 0) $max optional maximum returned value (default: mt_getrandmax ())

Here is used to let him randomly generate the background color, 0-255 random value. So the background color of the canvas is different without refreshing the page once.

Effect picture:

2.0 starts to make interference lines, interference points. Prevent the authentication image from being recognized in seconds

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 colors to draw a line segment in the image image from coordinates x1 to x2 (0,0 in the upper left corner of the image).

Imageline ($img, mt_rand (0150), mt_rand (0jue 50), mt_rand (0150), mt_rand (0pr 50), $linecolor); this means that the canvas $img is random from coordinates x1jingy1 to x2jiny2.

② imagesetpixel ()

Imagesetpixel- draws a single pixel

Bool imagesetpixel (resource $image, int $x, int $y, int $color) imagesetpixel () draws a point in the image image using the color color on the XMagi y coordinate (the upper left corner of the image is 0Magne0).

Imagesetpixel ($img, mt_rand (0150), mt_rand (0je 60), $dotcolor); the specific meaning is the same as the above effect:

3.0 add validation alphanumeric

Function:

Imagettftext ()

Imagettftext-writes text to the image in TrueType fonts

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 img 15), $x_start, 50 fontcolor, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key)

$img- canvas

25-size of the font.

Mt_rand (- 155.15)-the angle represented by the angle system, with 0 degrees as the text read from left to right. A higher value indicates a counterclockwise rotation. For example, 90 degrees represents text read from the bottom up. (it's a matter of font angle,)

The X axis position of the character is easy to understand.

50max 2-height of the character

$fontcolor- character color

"C:/Windows/Fonts/Verdana.TTF"-the font style path of the character

$key- traverses the characters

Effect:

At this point, the study on "how to use PHP to generate CAPTCHA" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Color image background validation generation function canvas interference character that is font analysis learning loop code coordinates size letter color effect Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info vpn Docker macOS