Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw graphic CAPTCHA through PHP

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to draw graphic CAPTCHA through PHP". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to draw graphic CAPTCHA through PHP".

Through our previous articles on image rendering in PHP, we can use the knowledge to expand, that is, we often use the graphic CAPTCHA on the daily login page, how the graphic CAPTCHA is formed, and if we want to learn how to make the graphic CAPTCHA, first let's take a look at how to draw text in PHP.

Draw text

The functions we often use to draw text are:

Imagechar (): draw a character horizontally

Imagecharup (): draw a character vertically

Imagestring (): draw a line of string horizontally

Imagestringup (): draw a line of string vertically

Imagettftext (): writes text to an image using TrueType fonts

Where the syntax formats of the imagechar (), imagecharup (), imagestring () and imagestringup () functions are the same, and their syntax formats are as follows:

Imagechar (resource $image, int $font, int $x, int $y, string $c, int $color)

Where $image indicates that the next operation of the created canvas is done on the canvas, $font represents the color of the font drawn, $x and $y represent the text drawn on the coordinates ($x ~ # _ color), $c represents the character (or string) to be drawn, and $color represents the color of the font to be drawn.

Examples are as follows:

Output result:

Another function is the imagettftext () function, which can also draw text, except that it can use TrueType fonts, which have an extension of Windows. A font in ttf format, which has the following syntax format:

Imagettftext (resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)

Where $size represents the size of the character, and $angle represents the angle, where 0 degrees represents the text read from left to right, increasing the value in the counterclockwise direction of rotation; $fontfile represents the path to the TrueType font file; and $textb represents the string to be drawn.

Examples are as follows:

Output result:

As a result, the text is drawn through the imagettftext () function.

Drawing verification code

After the study of drawing text and images before, we can draw the graphic CAPTCHA through the knowledge we have learned before, and we can often encounter the graphic CAPTCHA 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 this effect, you need to go through a few steps:

The first step is to prepare the canvas we need, that 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.

Examples are as follows:

Output result:

At this point, I believe you have a deeper understanding of "how to draw graphic CAPTCHA through PHP". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report