Antialiased filled Arcs/Ellipses for PHP (GD)
In many cases websites need dynamically created images: pie charts, rounded corners, menu buttons, etc. This list is endless. PHP, or more precisely the GD library, provides filled elliptical arcs and ellipses, but they are not antialiased. Therefore I have written a PHP function to render filled antialiased elliptical arcs or filled antialiased ellipses (as well as circles..) with PHP easily. Drawing these filled arcs is now a one-liner.
The antialiasing and supported transparency will improve the overall look of your website. You can also draw outlined antialiased arcs or ellipses if you use a simple trick: Draw an arc with the border color and now draw a slightly smaller arc with your background color over it.
Download
All three downloads are licensed under the MIT-License (included in the download archives).



Changelog
- Version 1.1
Improved the rendering speed by ~20%
Thanks to Matthias Mächler for fixing some errors (uninitialized variables, deprecated passing of $img reference).- Version 1.0
Complete rewrite featuring transparency support and improved rendering quality
- Version 0.5
First version of the script
Documentation
To use the function just include imageSmoothArc.php
- include ("imageSmoothArc.php");
The interface is
- imageSmoothArc (&$img, $cx, $cy, $w, $h, $color, $start, $stop)
$img is a GD true color image. Make sure that alphablending is enabled and antialiasing is disabled.
- $img = imageCreateTrueColor (WIDTH, HEIGHT);
- imagealphablending ($img, true);
$cx and $cy define the center of the ellipse in pixels. $w and $h are the width and height of the ellipse.
$color is an array of four values. The first three values are the RGB color. The last value is the alpha blending factor. RGB are in the range 0-255 and alpha is in the range 0-127. Create an array like this (example: light yellow, half transparent)
- $color = array (255, 255, 140, 64);
$start and $stop are the starting and ending angles of the elliptical arc. They are defined in radians but are not limited to the range 0...2PI. $start may be greater than $stop. If you want to convert betwen degrees and radians you can use the following formular.
- $radians = $degrees / 180.0 * M_PI;
Rendering high quality graphics with PHP and GD is now possible, but it can also slow down your server. I would recommend to redraw live data only if it changes significantly and then cache it on the harddisk. For example, a pie diagram, which should display user ratings must only be redrawn if a user votes and not everytime it will be sent out to a client.
Who uses it?
Iain Campbell used the imageSmoothArc to create a script which draws rounded corners. You may use it to apply those corners to html elements for smooth edges. The script is available here.
Philippe Bourcier creates nice pie diagrams for posters of his netlantis project.
Pavlin Georgiev presents a statistics diagram which you can test on the website.
Eric from Aken Inc. also likes the script.
Oliver Schneider draws the white and black stones of the board game Go with this script.
Josep Comajuncosas uses it to draw some circles on a map.
And last but not least, I am using it (a slightly modified version) to draw rounded corners for HTML div boxes.
If you have created something useful with the help of my script, I can put a link to your project on this page. Just leave a message.


