You can install the package via composer:
composer require choowx/rasterize-svgThis package relies on the sharp js package being available on your system. In most cases you can accomplish this by issue this command in your project.
npm install sharpuse Choowx\RasterizeSvg\Svg;
$svgString = '<svg width="100" height="100" viewBox="0 0 100 100"...';
$jpegBinaryString = Svg::make($svgString)->toJpeg();
$jpegBinaryString = Svg::make($svgString)->toJpg(); // Alias of toJpeg()
$pngBinaryString = Svg::make($svgString)->toPng();
$webpBinaryString = Svg::make($svgString)->toWebp();If you want to straight away save the rasterized image on disk:
use Choowx\RasterizeSvg\Svg;
$svgString = '<svg width="100" height="100" viewBox="0 0 100 100"...';
Svg::make($svgString)->saveAsJpeg('path/to/rasterized.jpeg');
Svg::make($svgString)->saveAsJpg('path/to/rasterized.jpg'); // Alias of saveAsJpeg()
Svg::make($svgString)->saveAsPng('path/to/rasterized.png');
Svg::make($svgString)->saveAsWebp('path/to/rasterized.webp');composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.