Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: creating a smooth edged circle, image or font?

I'm making a PHP image script that will create circles at a given radius.

I used:

<?php
imagefilledellipse ( $image, $cx, $cy, $w, $h, $color );
?>

but hate the rough edges it produces. So I was thinking of making or using a circle font that I will output using:

<?php
 imagettftext ( $image, $size, $angle, $x, $y, $color, 'fontfile.ttf', $text );
?>

So that the font will produce a circle that has a smooth edge. My problem is making the "font size" match the "radius size".

Any ideas? Or maybe a PHP class that will produce a smooth edge on a circle would be great!

Thank you.

like image 218
floatleft Avatar asked Mar 14 '10 23:03

floatleft


2 Answers

for quick and dirty anti-aliasing, make the image twice the desired size, then down-sample to the desired size.

$circleSize=90;
$canvasSize=100;

$imageX2 = imagecreatetruecolor($canvasSize*2, $canvasSize*2);

$bg = imagecolorallocate($imageX2, 255, 255, 255);

$col_ellipse = imagecolorallocate($imageX2, 204, 0, 0);

imagefilledellipse($imageX2, $canvasSize, $canvasSize, $circleSize*2, $circleSize*2, $col_ellipse);

$imageOut = imagecreatetruecolor($canvasSize, $canvasSize);
imagecopyresampled($imageOut, $imageX2, 0, 0, 0, 0, $canvasSize, $canvasSize, $canvasSize*2, $canvasSize*2);

header("Content-type: image/png");
imagepng($imageOut);
like image 77
Jon Avatar answered Oct 01 '22 16:10

Jon


Clever idea, I like that!

But maybe this PHP class already does the trick: 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.

like image 43
Pekka Avatar answered Oct 01 '22 18:10

Pekka