Z
Z
Zorgios2021-07-14 21:32:46
PHP
Zorgios, 2021-07-14 21:32:46

How to draw an arc for an SVG file using PHP?

To draw simple objects from lines and circles / ellipses, I use the php-svg

library. Now I have encountered a problem with drawing an arc (part of a circle).
For example, you need to draw half a circle, or a quarter (knowing the radius of the circle).
Now, to draw lines using the library, I use the following code:

$image = new SVG(10mm','10mm');
$doc = $image->getDocument();
$square = new SVGLine('0mm', '0mm', '5mm', '5mm'); // x0 y0 x1 y1
$square->setStyle('stroke', '#FF0000'); //цвет
$doc->addChild($square);
header('Content-Type: image/svg+xml');
echo $image;


What can you think of with half circles? How can circles be trimmed to a specific arc?
Not necessarily by means of a library. You can also use classic PHP

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vamp, 2021-07-14
@Zorgios

The easiest way to draw an arc is through the path element.

$image = new SVG('100mm','100mm');
$doc = $image->getDocument();
$square = new SVGLine('0mm', '0mm', '55mm', '55mm'); // x0 y0 x1 y1
$square->setStyle('stroke', '#FF0000'); //цвет
$doc->addChild($square);

$arc = new SVGPath('m 40,40 A 30,30,0,0,1,150,150');
$arc->setStyle('stroke', '#FF0000');
$doc->addChild($arc);

header('Content-Type: image/svg+xml');
echo $image;

You need to learn a little about the path format. He's not complicated.
m 40,40 A 30,30,0,0,1,150,150
m is the move command. Sets the "brush" to the specified position. Next command A - arc (arc). Its format is somewhat more complicated:
30.30 - the radius of the arc along the x and y axes
0 - the angle of rotation of the arc around the x axis
0 - whether the arc should be greater than 180 degrees or not
1 - in which direction to draw the arc - clockwise or counterclockwise
150,150 - coordinates arc end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question