php-svg
| Language | PHP (7.4+) |
|---|---|
| License | MIT |
| Repository | github.com/meyfa/php-svg |
| Documentation | README |
| Depends on | nothing; SimpleXML extension to load SVG, GD extension to rasterize |
| Read / write | yes / yes (rasterizes to GD images) |
php-svg represents an SVG image as a tree of node objects (SVGRect, SVGCircle, SVGPath, SVGText, SVGGroup, …). You can build that tree in code, load it from a file or string, query elements by id, tag or class, change attributes and styles, and convert it back to XML. It also has its own rasterizer that draws into a GD image, with a TrueType parser for text.
The README calls the library alpha quality, and the rasterizer in particular is incomplete: gradients, filters, masks and complex text may render wrong or not at all, and large images are slow. Generating and editing SVG works well; for accurate rasterization use Imagick or call rsvg-convert.
Installation#
composer require meyfa/php-svg
Basic usage: create an SVG file#
<?php
require __DIR__ . '/vendor/autoload.php';
use SVG\SVG;
use SVG\Nodes\Shapes\SVGCircle;
use SVG\Nodes\Shapes\SVGRect;
$image = new SVG(200, 100);
$doc = $image->getDocument();
$rect = new SVGRect(10, 10, 80, 80);
$rect->setStyle('fill', '#4a90e2');
$doc->addChild($rect);
$circle = (new SVGCircle(150, 50, 40))
->setStyle('fill', 'orange')
->setStyle('stroke', '#000')
->setStyle('stroke-width', '2px');
$doc->addChild($circle);
file_put_contents('out.svg', $image->toXMLString());
toXMLString() includes the <?xml ?> declaration; toXMLString(false) omits it, for inline SVG in HTML. echo $image prints the same as toXMLString().
How to load and modify an SVG file#
use SVG\SVG;
$image = SVG::fromFile('in.svg'); // or SVG::fromString($xml)
$doc = $image->getDocument();
$logo = $doc->getElementById('logo');
if ($logo !== null) {
$logo->setStyle('fill', 'green');
}
foreach ($doc->getElementsByTagName('circle') as $circle) {
$circle->setAttribute('stroke', 'black');
}
echo $doc->getWidth(), ' x ', $doc->getHeight(), "\n";
file_put_contents('modified.svg', $image->toXMLString());
How to convert SVG to PNG#
toRasterImage($width, $height) returns a GD image; encode it with GD functions:
use SVG\SVG;
$image = SVG::fromFile('in.svg');
$raster = $image->toRasterImage(400, 200); // transparent background
imagepng($raster, 'out.png');
How to convert SVG to JPEG with a background#
$raster = $image->toRasterImage(400, 200, '#FFFFFF');
imagejpeg($raster, 'out.jpg', 90);
How to render text#
The rasterizer needs TrueType fonts; register them before rendering:
SVG::addFont(__DIR__ . '/fonts/DejaVuSans.ttf');
SVG::addFont(__DIR__ . '/fonts/DejaVuSans-Bold.ttf');
imagepng($image->toRasterImage(400, 200), 'text.png');
Tips#
- Send SVG to a browser with
header('Content-Type: image/svg+xml'); echo $image;. SVG::fromFilealso accepts URLs; don’t pass user-supplied paths or URLs to it.- Child nodes can be added at a position (
addChild($node, 1)), removed (removeChild) and replaced (setChild).