arnapou/svg

Library - Help to draw svg images.

v1.0.1 2025-03-14 19:25 UTC

This package is auto-updated.

Last update: 2025-03-24 08:23:44 UTC


README

pipeline coverage

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

DateRef8.48.3
29/01/20251.0.x, main××