arnapou / svg
Library - Help to draw svg images.
v1.0.1
2025-03-14 19:25 UTC
Requires
- php: ~8.3.0 || ~8.4.0
- arnapou/geometry: ^1.0.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.52
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^2.0
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/php-code-coverage: ^11.0
- phpunit/phpunit: ^11.0
README
This library is a simple tool to generate SVG images.
It depends on arnapou/geometry for basic geometric shapes and transformations.
Installation
composer require arnapou/svg
packagist 👉️ arnapou/svg
Examples
basic.php
use Arnapou\Geometry\Element\Circle;
use Arnapou\Geometry\Element\Point;
use Arnapou\Geometry\Turtle;
use Arnapou\Svg\Color\NamedColor;
use Arnapou\Svg\SvgAttributeList\Fill;
use Arnapou\Svg\SvgAttributeList\Stroke;
use Arnapou\Svg\SvgAttributeList\SvgAttributes;
use Arnapou\Svg\SvgImage;
$style = new SvgAttributes(
Fill::transparent(),
new Stroke(NamedColor::blue, width: 5.0),
);
$image = new SvgImage(200, 100);
$image->children->add(new Circle(new Point(50, 50), radius: 20), $style);
$image->children->add(
Turtle::start(0, 0)
->forward(80)->turn(270, 10)
->forward(30)->turn(270, 10)
->forward(80)->turn(270, 10)
->forward(30)->turn(270, 10)
->getPath()
->rotate(M_PI / 12, new Point(0, 0))
->translate(90, 45),
$style,
);
$image->write(__DIR__ . '/basic.svg');
polyline.php
use Arnapou\Geometry\Element\Point;
use Arnapou\Geometry\Element\Polyline;
use Arnapou\Svg\Color\NamedColor;
use Arnapou\Svg\SvgAttribute\StrokeLineCap;
use Arnapou\Svg\SvgAttribute\StrokeLineJoin;
use Arnapou\Svg\SvgAttributeList\Fill;
use Arnapou\Svg\SvgAttributeList\Stroke;
use Arnapou\Svg\SvgImage;
$polyline = new Polyline(
[
new Point(0, 0),
new Point(10, 30),
new Point(20, 20),
new Point(35, 20),
new Point(50, 40),
new Point(40, 5),
new Point(20, 10),
],
closed: true,
);
$image = new SvgImage(200, 100);
$image->children->add(
$polyline,
Fill::transparent(),
new Stroke(NamedColor::red),
);
$image->children->add(
$polyline
->toPolyline(closed: false)
->rotate(M_PI_4, new Point())
->translate(100, 20),
new Fill(NamedColor::gray, opacity: 0.5),
new Stroke(NamedColor::darkblue, width: 5.0),
StrokeLineJoin::bevel,
StrokeLineCap::round,
);
$image->flipVertical()->write(__DIR__ . '/polyline.svg');
Php versions
Date | Ref | 8.4 | 8.3 |
---|---|---|---|
29/01/2025 | 1.0.x, main | × | × |