Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Svg Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require dev-arindam-roy/laravel-svg-package
    

    Publish the config (if needed):

    php artisan vendor:publish --provider="DevArindamRoy\SvgPackage\SvgPackageServiceProvider"
    
  2. 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');
    
  3. 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).


Implementation Patterns

Core Workflows

  1. On-the-Fly Conversion Use convertAndDownload() for immediate file generation:

    $svg = '<svg>...</svg>';
    SvgConverter::convertAndDownload($svg, 'custom-badge.png', 'image/png');
    
  2. Storage Integration Save converted images to storage (e.g., public or storage/app):

    $path = SvgConverter::convertAndStore($svg, 'storage/app/svgs/', 'profile-icon.png');
    
  3. URL Generation Generate a temporary URL for the converted image:

    $url = SvgConverter::convertAndGetUrl($svg, 'temp-icon.png', 3600); // Expires in 1 hour
    

Advanced Patterns

  • 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);
    }
    

Gotchas and Tips

Common Pitfalls

  1. Dependency Conflicts Ensure imagick or gd PHP extensions are installed (required for conversion).

    • Debug: Check phpinfo() for Imagick or GD support.
    • Fix: Install via pecl install imagick or enable extension=gd in php.ini.
  2. File Permissions If storing files, ensure the target directory is writable:

    chmod -R 755 storage/app/svgs
    
  3. 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");
    }
    
  4. Memory Limits Large SVGs may hit PHP memory limits. Adjust memory_limit in php.ini or optimize SVG complexity.

Debugging Tips

  • 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.

Extension Points

  1. Custom Formats Extend the package by adding support for new formats (e.g., WebP) by modifying the convert() method in SvgConverter.

  2. Hooks for Post-Conversion Use Laravel events to trigger actions after conversion:

    // In EventServiceProvider
    protected $listen = [
       'svg.converted' => [SvgPostProcessor::class, 'handle'],
    ];
    
  3. Queue Delayed Conversions Offload heavy conversions to a queue (e.g., svg-convert:delayed job):

    SvgConverter::queueConversion($svg, 'output.png', now()->addMinutes(5));
    
  4. 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'),
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver