Get the App
SLTechnology News&Howtos  ›  Development  › 

How to generate and make CAPTCHA for PHP

Shulou Source: shulou.com Published: 2022-06-03 20:22:10 11月12日 Update

This article will explain in detail how to generate and produce CAPTCHA for you. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

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

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 XMagol y coordinate (the upper left corner of the image is 0Pol 0).

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

Effect picture:

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:

This is the end of the article on "how to generate the verification code for PHP". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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