pavlm / ffi-librsvg
SVG rasterization library binding
Installs: 1 435
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ~7.4 | ~8.0
- ext-ffi: *
This package is auto-updated.
Last update: 2025-05-05 11:02:11 UTC
README
PHP binding for librsvg2
. It allows conversion (rasterisation) of SVG image to PNG, with minimal dependencies.
Why
Recommended way to convert SVG in PHP is by using the ImageMagick extension. However, its SVG functionality may be limited depending on your system libraries. The actual conversion process is performed by imagemagick-6.q16
, which has optional SVG support. For full SVG support, proper compilation is required, and in the background, it utilizes librsvg2
.
There are alternative conversion methods available, but they require cross-process communication.
Installation
sudo apt install librsvg2-2
composer require pavlm/ffi-librsvg
Usage
<?php use pavlm\Rsvg\Rsvg; require 'vendor/autoload.php'; $svgData = '<svg viewBox="0 0 100 100"> <rect x="10" y="10" width="80" height="80" fill="green" /> </svg>'; print Rsvg::convertSvg2Png($svgData);
Conversion via command line utility rsvg-cli.php
curl https://upload.wikimedia.org/wikipedia/commons/f/fd/Ghostscript_Tiger.svg | \
tee /tmp/tiger.svg | \
php bin/rsvg-cli.php | \
tee /tmp/tiger.png | \
display