AtelierRasterizer
SVG to pixels. The one that leaves the vector world.
A typed PHP abstraction over a rasterizer binary. Hand it SVG markup, get PNG bytes back. Temporary files, process timeouts, and binary resolution are handled for you; the rendering itself is done by resvg or rsvg-convert.
Features
- Takes SVG in any form
A markup string, any\Stringablesuch as anSvgdocument, or a file or stream throughSvgInput. - Two adapters, one interface
resvgandrsvg-convertbehindRasterizerInterface, resolved automatically or pinned when output has to stay reproducible across machines. - Typed results, typed failures
ABitmapResultcarries the PNG bytes, the format, the dimensions and the MIME type. Every failure implements one exception interface, and the timeout is an option.
Rasterize a document
use Atelier\Rasterizer\Rasterizer;
$bitmap = Rasterizer::create()->rasterize($svg);
$bitmap->save('icon.png');
$svg is a markup string, any \Stringable, or an SvgInput. An Atelier\Svg\Svg document is \Stringable, so it passes straight through:
use Atelier\Svg\Svg;
$document = Svg::create(120, 120);
Rasterizer::create()->rasterize($document)->save('logo.png');
For a file or a stream, name the source explicitly. A plain string is always markup, never a path:
use Atelier\Rasterizer\Svg\SvgInput;
$rasterizer->rasterize(SvgInput::fromFile('logo.svg'));
$rasterizer->rasterize(SvgInput::fromStream($handle));
Set size, scale, and background
use Atelier\Rasterizer\Bitmap\BitmapOptions;
$bitmap = $rasterizer->rasterize($svg, new BitmapOptions(
width: 1200,
height: 630,
keepAspectRatio: true,
background: '#ffffff',
timeout: 30.0,
));
$bitmap->contents; // raw PNG bytes
$bitmap->mimeType; // 'image/png'
$bitmap->width;
$bitmap->height;
Every field has a default, so new BitmapOptions() is valid. Set one dimension to derive the other from the document ratio, or scale to render at a multiple of the intrinsic size for high-DPI output.
Pick an adapter
Rasterizer::create() returns the first available adapter in package order: resvg, then rsvg-convert. Pin one when output must stay reproducible across machines:
$rasterizer = Rasterizer::resvg();
$rasterizer = Rasterizer::rsvgConvert();
Both adapters emit PNG. They differ in filter and font handling, so switching adapters can change the rendered result.
Handle failures
Every failure implements Atelier\Rasterizer\Exception\ExceptionInterface:
use Atelier\Rasterizer\Exception\BinaryNotFoundException;
use Atelier\Rasterizer\Exception\ExceptionInterface;
try {
$bitmap = $rasterizer->rasterize($svg);
} catch (BinaryNotFoundException $e) {
// No resvg or rsvg-convert on this host.
} catch (ExceptionInterface $e) {
// Invalid input, unsupported format, process failure or timeout.
}
Documentation
Start
- Installation: install the package and an available rendering binary.
- Usage: rasterize markup, documents, files, and streams.
Control the result
- Options: set dimensions, scale, aspect ratio, background, and timeout.
Adapters
- Adapters: choose automatically or pin a renderer for reproducible output.
- resvg: configure and use the resvg adapter.
- rsvg-convert: configure and use the librsvg adapter.
Reliability
- Testing: fake rasterization in unit tests and exercise a real adapter in integration tests.