When making CAPTCHA verification code, it is an important technical difficulty to ensure that the verification code can not only prevent the machine from automatically identifying it, but also enable users to easily identify it. PHP provides a rich image processing function, where imageantialias() can effectively improve the anti-aliasing effect of images, thereby making the edges of text or graphics in the verification code smoother and enhance readability.
This article will combine PHP's GD library to demonstrate how to use the imageantialias() function to improve the visual effect of the verification code when generating the CAPTCHA verification code.
imageantialias() is a function in the PHP GD library to turn on or off the anti-aliasing effect of images. Jagging is because the edges of the image have unsmoothed stepped pixel boundaries. Anti-aliasing makes the edges look smoother by smoothing the edge pixels.
The function prototype is as follows:
bool imageantialias ( resource $image , bool $enable )
$image : Image resource handle
$enable : Turn on or off the anti-aliasing effect, true is on, false is off
Let's first create a basic verification code picture:
<?php
// Create a picture 150x50 Blank image
$image = imagecreatetruecolor(150, 50);
// Assign colors
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
// Filled background with white
imagefilledrectangle($image, 0, 0, 150, 50, $white);
When antialiasing is enabled, the drawn lines and text edges will be smoother:
// Turn on anti-aliasing
imageantialias($image, true);
Add interference lines to the verification code to prevent simple OCR recognition:
for ($i = 0; $i < 5; $i++) {
$line_color = imagecolorallocate($image, rand(100, 150), rand(100, 150), rand(100, 150));
imageline($image, rand(0, 150), rand(0, 50), rand(0, 150), rand(0, 50), $line_color);
}
Here we use the imagettftext() function to draw text, and use anti-aliasing to make the text clearer:
$font = __DIR__ . '/fonts/arial.ttf'; // Please make sure the font path is correct
$code = substr(str_shuffle('ABCDEFGHJKLMNPQRSTUVWXYZ23456789'), 0, 5);
for ($i = 0; $i < strlen($code); $i++) {
$text_color = imagecolorallocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
imagettftext(
$image,
20,
rand(-15, 15),
20 + $i * 25,
rand(30, 40),
$text_color,
$font,
$code[$i]
);
}
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>
imageantialias() only works on drawn lines and shapes, and imagettftext() rendered with anti-aliasing effects are already built-in.
Turning on antialiasing may increase CPU burden slightly, but the improved image quality is usually worth it.
Make sure the font file exists and the path is correct, otherwise the text will not display normally.
<?php
// Create an image
$image = imagecreatetruecolor(150, 50);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, 150, 50, $white);
// Enable anti-aliasing
imageantialias($image, true);
// Draw disturbance lines
for ($i = 0; $i < 5; $i++) {
$line_color = imagecolorallocate($image, rand(100, 150), rand(100, 150), rand(100, 150));
imageline($image, rand(0, 150), rand(0, 50), rand(0, 150), rand(0, 50), $line_color);
}
// Verification code text
$font = __DIR__ . '/fonts/arial.ttf';
$code = substr(str_shuffle('ABCDEFGHJKLMNPQRSTUVWXYZ23456789'), 0, 5);
for ($i = 0; $i < strlen($code); $i++) {
$text_color = imagecolorallocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
imagettftext($image, 20, rand(-15, 15), 20 + $i * 25, rand(30, 40), $text_color, $font, $code[$i]);
}
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>
Through the above example, you can clearly see the smooth improvement of imageantialias() on the edges of the line in the image, thereby improving the overall readability of the CAPTCHA verification code. Combining interference lines and deformed text can effectively improve the security and user experience of verification codes.
If you need to access relevant resources or APIs, please note that you replace the URL domain name with m66.net , for example: