choowx / rasterize-svg
A PHP library for converting SVG to JPEG, PNG, and WEBP
Fund package maintenance!
choowx
Installs: 2 944
Dependents: 0
Suggesters: 0
Security: 0
Stars: 12
Watchers: 1
Forks: 6
Open Issues: 1
Requires
- php: ^8.1
- spatie/temporary-directory: ^2.1
- symfony/process: ^6.2|^7.0
Requires (Dev)
- laravel/pint: ^1.0
- pestphp/pest: ^2.15
- sapientpro/image-comparator: ^1.0
This package is not auto-updated.
Last update: 2025-01-06 11:13:38 UTC
README
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.