When processing images in PHP, the imageantialias() function is used to turn on or off the anti-aliasing effect. Anti-aliasing can smooth the edges of lines, avoid jagged edges, and enhance the aesthetics of the image. However, many developers will find that the image becomes blurry after using imageantialias() , which is not even as clear as when anti-aliasing is not turned on. Why does this happen? How to solve it?
imageantialias(resource $image, bool $enabled): bool
This is a function in the PHP GD library that sets whether to enable anti-aliasing on drawn lines. This function mainly acts on smoothing when drawing lines, rectangles and polygons.
Example:
<?php
$image = imagecreatetruecolor(200, 200);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, 199, 199, $white);
imageantialias($image, true); // Turn on anti-aliasing
imageline($image, 10, 10, 190, 190, $black);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>
The essence of anti-aliasing is smooth edges <br> Anti-aliasing achieves a smoothing effect by adding translucent pixels to the edges, which are between the foreground and background colors, resulting in lines that don't look that sharp, giving a "blurred" feeling.
Low-resolution images enlarge the blurred feeling <br> In low-resolution images, anti-aliased translucent pixels will be more obvious, especially thin lines, which are more likely to appear unclear.
Valid only for lines and shapes, not for bitmap images
imageantialias() is only effective for drawn vector lines and will not increase the sharpness of the bitmaps such as photos in the image. On the contrary, the overall look is a bit blurred due to the addition of translucent pixels.
Anti-aliasing works better on high-resolution images, you can consider creating a larger canvas for drawing first, and then reducing the image output.
<?php
$width = 400;
$height = 400;
$image = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $white);
imageantialias($image, true);
imageline($image, 20, 20, 380, 380, $black);
$final = imagecreatetruecolor($width / 2, $height / 2);
imagecopyresampled($final, $image, 0, 0, 0, 0, $width / 2, $height / 2, $width, $height);
header('Content-Type: image/png');
imagepng($final);
imagedestroy($image);
imagedestroy($final);
?>
Extremely thin lines are more likely to blur when anti-aliased, and properly bold lines can make the edges clearer.
<?php
imagesetthickness($image, 3); // Set the line width to3
imageline($image, 10, 10, 190, 190, $black);
?>
PHP's GD library has limited anti-aliasing capabilities. If higher quality image processing is required, you can consider using libraries such as ImageMagick to support more advanced anti-aliasing and image processing functions.
Anti-aliasing is only effective for drawing operations and is invalid for blurring problems with existing bitmap pictures. When processing photos, try to avoid using imageantialias() .