Skip to content

choowx/rasterize-svg

Repository files navigation

A PHP library for converting SVG to JPEG, PNG, and WEBP

Latest Version on Packagist Tests Total Downloads

Installation

You can install the package via composer:

composer require choowx/rasterize-svg

This 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 sharp

Usage

use 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');

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.