Current Location: Home> Latest Articles> How to Achieve Anti-Aliasing Effect When Drawing Circles with imageellipse() Using imageantialias Function

How to Achieve Anti-Aliasing Effect When Drawing Circles with imageellipse() Using imageantialias Function

M66 2025-06-23

Before introducing the implementation method, let’s first understand two key functions:

  • imageellipse(resource $image, int $cx, int $cy, int $width, int $height, int $color): Draws an ellipse on the image.

  • imageantialias(resource $image, bool $enabled): Turns anti-aliasing on or off.

However, it’s important to note that imageantialias() only works on graphics drawn using functions like imageline(), imagerectangle(), etc., and does not affect imageellipse(). This is a pitfall that many developers overlook.

So, how can we achieve anti-aliasing when drawing circles?

We need to change our strategy and either draw the graphic at a higher resolution and then shrink the image or manually simulate the anti-aliasing effect. Below is a common approach: Drawing at high resolution + scaling. This is a simple way to achieve anti-aliasing without the need for additional graphic libraries.

Example Code

<?php  
// Set the scale factor, e.g., drawing at 3x resolution  
$scale = 3;  
$width = 200;  
$height = 200;  
<p>$scaledWidth = $width * $scale;<br>
$scaledHeight = $height * $scale;</p>
<p>// Create a large image resource<br>
$largeImage = imagecreatetruecolor($scaledWidth, $scaledHeight);<br>
imageantialias($largeImage, true);</p>
<p>// Set the background color to white and fill it<br>
$white = imagecolorallocate($largeImage, 255, 255, 255);<br>
imagefill($largeImage, 0, 0, $white);</p>
<p>// Set the circle color<br>
$circleColor = imagecolorallocate($largeImage, 0, 0, 255);</p>
<p>// Draw the ellipse (which is actually a circle)<br>
$cx = $scaledWidth / 2;<br>
$cy = $scaledHeight / 2;<br>
$radius = min($scaledWidth, $scaledHeight) * 0.4;<br>
imageellipse($largeImage, $cx, $cy, $radius * 2, $radius * 2, $circleColor);</p>
<p>// Create the final smaller image resource<br>
$finalImage = imagecreatetruecolor($width, $height);</p>
<p>// Scale the image to achieve the anti-aliasing effect<br>
imagecopyresampled($finalImage, $largeImage, 0, 0, 0, 0, $width, $height, $scaledWidth, $scaledHeight);</p>
<p>// Output the image<br>
header('Content-Type: image/png');<br>
imagepng($finalImage);</p>
<p>// Clean up memory<br>
imagedestroy($largeImage);<br>
imagedestroy($finalImage);<br>
?><br>

Result and Explanation

In this example, we first draw a larger circle at 3 times the original size and then use imagecopyresampled() to scale the image down to the target size. This process is equivalent to simulating the anti-aliasing effect, and the resulting image edges will appear smoother.

Although imageantialias() does not work with imageellipse(), we can bypass this limitation using the scaling strategy to generate higher-quality images. If you have higher image processing requirements, consider using a professional image processing library like ImageMagick.

Further Suggestions

If you wish to save the image to the server instead of outputting it to the browser, you can do so like this:

imagepng($finalImage, '/path/to/save/image.png');  

Or generate a direct access link:

$filename = 'https://m66.net/images/circle.png';  
imagepng($finalImage, '/var/www/m66.net/images/circle.png');  

Make sure that the m66.net path has write permissions.

This scaling method is also applicable to other shapes like polygons, freehand curves, etc., and is a common anti-aliasing technique within the GD library.