Home > Backend Development > PHP Tutorial > Generate image verification code using PHP and GD library

Generate image verification code using PHP and GD library

WBOY
Release: 2023-05-11 09:34:01
Original
1054 people have browsed it

With the development of the Internet, more and more websites need to use verification codes to prevent malicious registration, crawlers and other behaviors. Image verification code is a common form of verification code. It displays an image containing random characters or numbers, allowing users to enter the correct verification code before proceeding to the next step. This article will introduce how to use PHP and GD library to generate image verification codes.

The GD library is an image processing library that can be used to generate, process and manipulate various image formats. PHP has built-in GD library and provides many functions for creating and manipulating images, so it is very convenient to use PHP and GD library to generate image verification codes.

Step 1: Generate verification code text

First, we need to generate a random string, which will be displayed on the verification code image. You can use PHP's built-in functions rand() and chr() to generate ASCII characters of a specified length:

$code = '';
$length = 4;
for ($i = 0; $i < $length; $i++) {
    $code .= chr(rand(97, 122));
}
Copy after login

The above code uses the ASCII code value to generate a four-character random string between 97 and 122. You can modify this code to generate a string containing numbers, or to increase the length of the string, according to your needs.

Step 2: Create a canvas

After generating the verification code text, we need to create a canvas to display it. Use the PHP built-in function imagecreate() to create a canvas with a specified width and height:

$width = 120;
$height = 40;
$image = imagecreate($width, $height);
Copy after login

The above code creates a canvas $image with a width of 120 pixels and a height of 40 pixels.

Step 3: Set background color and text color

In order to make the verification code image easier to recognize, we need to set some colors, including background color, text color, etc. Create a color using the PHP built-in function imagecolorallocate():

$bg_color = imagecolorallocate($image, 255, 255, 255); // 将背景色设置为白色
$text_color = imagecolorallocate($image, 0, 0, 0); // 将文字颜色设置为黑色
Copy after login

The above code sets the background color to white and the text color to black. You can modify the color value according to your needs.

Step 4: Add interference lines and noise points

In order to increase the difficulty of the verification code, we can add some interference lines and noise points. Use the PHP built-in function imageline() to create interference lines:

for ($i = 0; $i < 5; $i++) {
    $line_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); // 生成一种随机颜色
    imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $line_color); // 在随机位置生成一条干扰线
}
Copy after login

The above code generates 5 random interference lines. You can increase or decrease the number of interference lines according to your needs.

Use the PHP built-in function imagesetpixel() to create noise points:

for ($i = 0; $i < 100; $i++) {
    $noise_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); // 生成一种随机颜色
    imagesetpixel($image, rand(0, $width), rand(0, $height), $noise_color); // 在随机位置生成一个噪点
}
Copy after login

The above code generates 100 random noise points. You can increase or decrease the amount of noise according to your needs.

Step 5: Add text

Finally, we need to add the generated verification code text to the canvas. Use the PHP built-in function imagettftext() to create text:

$font_path = 'font.ttf'; // 字体文件路径
$font_size = 20; // 字体大小
$x = 20; // 文本起始横坐标
$y = 30; //文本起始纵坐标
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font_path, $code); // 将验证码文本添加到画布上
Copy after login

The above code uses the font file font.ttf to create a text of size 20, and adds the text to the specified position of the canvas.

Step 6: Output image

Finally, we need to output the generated verification code image to the browser or save it to a file. Use the PHP built-in function imagepng() to output the image to the browser:

header('Content-Type: image/png');
imagepng($image);
Copy after login

The above code sets the output type to image/png and outputs the canvas $image to the browser in PNG format. Other image formats such as JPEG, GIF, etc. can also be used.

The complete code is as follows:

$code = '';
$length = 4;
for ($i = 0; $i < $length; $i++) {
    $code .= chr(rand(97, 122));
}

$width = 120;
$height = 40;
$image = imagecreate($width, $height);

$bg_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);

for ($i = 0; $i < 5; $i++) {
    $line_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $line_color);
}

for ($i = 0; $i < 100; $i++) {
    $noise_color = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($image, rand(0, $width), rand(0, $height), $noise_color);
}

$font_path = 'font.ttf';
$font_size = 20;
$x = 20;
$y = 30;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font_path, $code);

header('Content-Type: image/png');
imagepng($image);
Copy after login

It is very simple to generate image verification code using PHP and GD library, just follow the above steps. For those with a certain programming foundation, the generation process can be further optimized, such as using more complex character or number combinations, custom fonts, etc. But no matter what type of image verification code it is, it is a basic tool to prevent online fraud and has made an important contribution to the construction of Internet security.

The above is the detailed content of Generate image verification code using PHP and GD library. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template