Home>Article>Backend Development> How to draw graphical verification code through PHP?

How to draw graphical verification code through PHP?

WBOY
WBOY Original
2021-10-20 10:18:07 2026browse

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!

How to draw graphical verification code through PHP?

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

Among them, the syntax format of imagechar(), imagecharup(), imagestring() and imagestringup() functions is the same. Their syntax format is as follows:

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.


The example is as follows:

Output result:


How to draw graphical verification code through PHP?

There is also a function called imagettftext() function, It can also draw text, but the difference is that it can use TrueType fonts, which are extensions in Windows systems. Ttf format font, its syntax format is as follows:


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.


The example is as follows:

Output result:


How to draw graphical verification code through PHP?##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:


How to draw graphical verification code through PHP?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:


Output result:


How to draw graphical verification code through PHP?How to draw graphical verification code through PHP?How to draw graphical verification code through PHP?Recommended study:《

PHP video tutorial

The 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn