Home>Article>Backend Development> How to draw graphical verification code through PHP?
In the previous article, I brought you "How to draw polygons, arcs and ellipses in PHP? (Detailed graphic explanation)", which introduces in detail how to draw graphics in PHP. In this article, we will take a look at how we generate graphic verification codes in PHP. I hope everyone has to help!
Through our previous article on image drawing in PHP, we can use the knowledge to make an extension, which is the graphical verification we often use on daily login pages. code, how is the graphic verification code formed? If you want to learn how to make the graphic verification code, first let's take a look at how to draw text in PHP.
Drawing text
The functions we commonly use to draw text are:
imagechar()
: Draw a character horizontally
imagecharup()
: Draw a character vertically Characters
#imagestring(): Draw a line of string horizontally
imagestringup(): Draw a line of string vertically
imagettftext(): Use TrueType font to image Write text in
imagechar(resource $image, int $font, int $x, int $y, string $c, int $color)Among them, $image represents the created canvas. The next operations are performed on the canvas. $font represents the color of the drawn font. $x and $y represent the coordinates ($x, $y). Draw text at the position, $c represents the character (or string) to be drawn, and $color represents the color of the drawn font.
imagettftext(resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)Among them, $size represents the size of the character, $angle represents the angle, 0 degrees in this angle represents the text read from left to right, press The value increases in the counterclockwise direction; $fontfile represents the path to the TrueType font file to be used; $textb represents the string to be drawn.
##This is completed through the imagettftext() function Drawing of text.
Draw graphics verification codeAfter the previous learning of drawing text and images, we can then draw graphics using the knowledge we have learned before. Drawing of verification codes, we often encounter graphical verification codes in our login interface, so let's take a look at how to create it.
First, let’s take a look at the final effect:
To achieve such an effect, you need to go through some steps:
The first step is to prepare the canvas we need, which is to create the canvas. Then prepare the string we want to generate, draw the interference pattern on the canvas, then output the image, and finally release the resources.
The example is as follows:
Recommended study:《
PHP video tutorialThe above is the detailed content of How to draw graphical verification code through PHP?. For more information, please follow other related articles on the PHP Chinese website!