Home > Article > Backend Development > Tips for generating colorful verification code images using PHP and GD library
Tips for generating colored verification code images using PHP and GD libraries
Introduction:
Verification code is a common network security technology. By requiring users to enter a verification code when logging in, registering, or submitting a form, you can effectively prevent automated attacks from robots and malicious programs. This article will introduce the techniques of using PHP and GD libraries to generate colorful verification code images, helping developers to add a certain degree of recognizability and artistry when creating verification codes.
1. Environment preparation
Before you start, make sure that PHP and GD libraries have been installed in your development environment. You can install the GD library by entering the following command in the terminal:
sudo apt-get update sudo apt-get install php7.4-gd
2. Generate interference lines
Before generating the verification code, we can first generate some interference lines to increase the complexity and reliability of the verification code. Readability. The following is a sample code for generating interference lines:
// 创建一个画布 $width = 120; $height = 40; $image = imagecreatetruecolor($width, $height); // 生成两条随机干扰线 for ($i = 0; $i < 2; $i++) { $lineColor = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imageline($image, 0, mt_rand(0, $height), $width, mt_rand(0, $height), $lineColor); } // 输出图片 header("Content-type: image/png"); imagepng($image); imagedestroy($image);
3. Generate verification code
Next, we can start generating verification codes. The following is a sample code that generates a four-digit verification code:
session_start(); $width = 120; $height = 40; $length = 4; $font = 'path/to/your/font.ttf'; // 设置字体文件的路径 // 创建一个画布 $image = imagecreatetruecolor($width, $height); // 设置背景颜色 $bgColor = imagecolorallocate($image, 255, 255, 255); imagefilledrectangle($image, 0, 0, $width, $height, $bgColor); // 生成四位数字验证码 $code = ''; for ($i = 0; $i < $length; $i++) { $textColor = imagecolorallocate($image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200)); $digit = mt_rand(0, 9); $code .= $digit; imagettftext($image, 20, mt_rand(-30, 30), ($width / $length) * $i + 10, ($height + 20) / 2, $textColor, $font, $digit); } // 保存验证码到session中 $_SESSION['captcha'] = $code; // 输出图片 header("Content-type: image/png"); imagepng($image); imagedestroy($image);
4. Use verification code
In your form, you can add an input box for the user to enter the verification code and verify it in the background Whether the verification code entered by the user is consistent with the previously generated verification code. The following is a simple example:
<form action="submit.php" method="post"> <label for="captcha">验证码:</label> <input type="text" id="captcha" name="captcha" required> <img src="captcha.php" alt="验证码"> <input type="submit" value="提交"> </form>
And verify the verification code in submit.php:
session_start(); if ($_POST['captcha'] == $_SESSION['captcha']) { // 验证码正确,进行后续操作 } else { // 验证码错误,给出相应提示 }
Conclusion:
This article introduces the use of PHP and GD libraries to generate colored verification codes The image trick, by generating distracting lines and randomizing the font color, increases the complexity and readability of the CAPTCHA. Hopefully these tips will help developers create more secure and beautiful captchas.
The above is the detailed content of Tips for generating colorful verification code images using PHP and GD library. For more information, please follow other related articles on the PHP Chinese website!