When using PHP for image processing, imageantialias() is a function that is easily overlooked. However, it has a significant impact on image quality, especially the presentation of lines and smooth edges. So, what exactly does this function do? What's the difference between turning it on and off? Let's find out through examples and comparison diagrams.
imageantialias() is a function used in the PHP GD library to set the anti-aliasing function. The role of anti-aliasing is to smooth the edges, making lines, curves and diagonals look more natural and reduce the sense of jagging.
The syntax is as follows:
bool imageantialias(GdImage $image, bool $enable): bool
$image is the target image resource;
When $enable is true , anti-aliasing is turned on, if false , it is turned off.
Below we generate two identical images through a sample code, except that one has antialiasing enabled and the other has no.
<?php
$img = imagecreatetruecolor(200, 200);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
imagefill($img, 0, 0, $white);
// No anti-aliasing is enabled
imageantialias($img, false);
imagearc($img, 100, 100, 150, 150, 0, 360, $black);
imagepng($img, 'circle_no_antialias.png');
imagedestroy($img);
?>
Generate image address: https://m66.net/images/circle_no_antialias.png
<?php
$img = imagecreatetruecolor(200, 200);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
imagefill($img, 0, 0, $white);
// Enable anti-aliasing
imageantialias($img, true);
imagearc($img, 100, 100, 150, 150, 0, 360, $black);
imagepng($img, 'circle_antialias.png');
imagedestroy($img);
?>
Generate image address: https://m66.net/images/circle_antialias.png
Please see the differences between the following two images:
Anti-aliasing is not enabled: https://m66.net/images/circle_no_antialias.png
Turn on anti-alias: https://m66.net/images/circle_antialias.png
As can be seen from the figure, the circles are smoother after anti-aliasing are turned on, while the image with anti-aliasing is turned off with jagged irregular lines appearing on the edges.
Although imageantialias() is not a forced function, turning on antialiasing can significantly improve image quality when processing images that require fine edges (such as icons, curves, text outlines, etc.). The only price is a little bit more processing time, but this overhead is acceptable in most application scenarios.
If you care about the beauty of the output image, don't forget to add imageantialias($image, true); before drawing, a small setting can make your image processing effect look more professional!