arindam/svg
Laravel package to convert raw SVG markup into .svg files. Save with optional filename, download as a response, or render directly with correct SVG content-type. Supports Laravel with/without package auto-discovery via service provider and facade.
Installation
composer require dev-arindam-roy/laravel-svg-package
Publish the config (if needed):
php artisan vendor:publish --provider="DevArindamRoy\SvgPackage\SvgPackageServiceProvider"
Basic Usage Convert SVG string to an image (PNG/JPEG) and download:
use DevArindamRoy\SvgPackage\Facades\SvgConverter;
$svgString = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" fill="red"/></svg>';
SvgConverter::convertAndDownload($svgString, 'output.png');
First Use Case Dynamically generate a badge or icon from SVG markup and serve it as a downloadable file in a user request (e.g., after a form submission).
On-the-Fly Conversion
Use convertAndDownload() for immediate file generation:
$svg = '<svg>...</svg>';
SvgConverter::convertAndDownload($svg, 'custom-badge.png', 'image/png');
Storage Integration
Save converted images to storage (e.g., public or storage/app):
$path = SvgConverter::convertAndStore($svg, 'storage/app/svgs/', 'profile-icon.png');
URL Generation Generate a temporary URL for the converted image:
$url = SvgConverter::convertAndGetUrl($svg, 'temp-icon.png', 3600); // Expires in 1 hour
Dynamic SVG Generation Combine with Blade or PHP to generate SVGs dynamically:
$svg = view('svg.templates.badge', ['text' => 'New'])->render();
SvgConverter::convertAndDownload($svg, 'badge.png');
Batch Processing Process multiple SVGs in a loop (e.g., for bulk exports):
foreach ($svgFiles as $file) {
$svgContent = file_get_contents($file['path']);
SvgConverter::convertAndStore($svgContent, 'outputs/', $file['name'] . '.png');
}
Middleware for SVG-to-Image Create middleware to auto-convert SVGs in routes:
// app/Http/Middleware/ConvertSvgToImage.php
public function handle($request, Closure $next) {
if ($request->has('svg') && $request->wantsJson()) {
return SvgConverter::convertAndGetUrl($request->svg, 'dynamic.png');
}
return $next($request);
}
Dependency Conflicts
Ensure imagick or gd PHP extensions are installed (required for conversion).
phpinfo() for Imagick or GD support.pecl install imagick or enable extension=gd in php.ini.File Permissions If storing files, ensure the target directory is writable:
chmod -R 755 storage/app/svgs
SVG Validation Malformed SVG markup may cause silent failures. Validate SVGs first:
$dom = new DOMDocument();
if (!$dom->loadXML($svgString)) {
throw new \Exception("Invalid SVG markup");
}
Memory Limits
Large SVGs may hit PHP memory limits. Adjust memory_limit in php.ini or optimize SVG complexity.
Log Conversion Errors
Enable debug mode in config/svg-package.php:
'debug' => env('APP_DEBUG', false),
Errors will log to storage/logs/laravel.log.
Test with Minimal SVGs
Start with simple SVGs (e.g., <svg><circle/></svg>) to isolate issues.
Custom Formats
Extend the package by adding support for new formats (e.g., WebP) by modifying the convert() method in SvgConverter.
Hooks for Post-Conversion Use Laravel events to trigger actions after conversion:
// In EventServiceProvider
protected $listen = [
'svg.converted' => [SvgPostProcessor::class, 'handle'],
];
Queue Delayed Conversions
Offload heavy conversions to a queue (e.g., svg-convert:delayed job):
SvgConverter::queueConversion($svg, 'output.png', now()->addMinutes(5));
Override Default Config
Customize paths, formats, or quality settings in config/svg-package.php:
'default_format' => 'jpeg',
'quality' => 90,
'storage_path' => storage_path('app/svg-outputs'),
How can I help you explore Laravel packages today?