mmmint mmmint - 5 months ago 23
PHP Question

Arc draw with imperfections

This is my php file (arc.php) which should produce a thick half arc:

<?php
$img = imagecreatetruecolor(2000, 1000);
$white = imagecolorallocate($img, 255, 255, 255);
imagesetthickness($img, 200);
imagearc($img, 1000, 1000, 1900, 1900, 180, 360, $white);
imagepng($img);
imagedestroy($img);





The image is generated via cli:

php arc.php > arc.png




So far, so good...Now, does somebody know why do I get those imperfections in the image.


enter image description here

Answer

The comments in the documentation for imagesetthickness seem to imply that ellipses and arcs don't play nice with thickness settings. Whether or not this is a bug or a feature is debateable. Regardless, the suggested workaround is using a small thickness and repeatedly drawing the shape larger and larger.

In your case, it would look something like:

<?php
$img = imagecreatetruecolor(2000, 1000);
$white = imagecolorallocate($img, 255, 255, 255);

// Slightly thicker than 1 pixel to compensate pixel aliasing
imagesetthickness($img, 2);

$thickness = 200;
for ($i = $thickness; $i > 0; $i--) {
    imagearc($img, 1000, 1000, 1900 - $i, 1900 - $i,  180, 360, $white);
}

imagepng($img);
imagedestroy($img);

Which generates the following image:enter image description here
You will probably need to adjust the results a bit, but this should be enough to get you started down the correct path.

Comments