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

Resmushit Laravel Package

golchha21/resmushit

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:
    composer require golchha21/resmushit
    php artisan vendor:publish --provider="Golchha21\ReSmushIt\Providers\ServiceProvider" --tag=config
    
  2. Configure config/ReSmushIt.php with your API key (add 'api_key' => 'your_api_key').
  3. First Use Case: Optimize a single image:
    use Golchha21\ReSmushIt\Facades\ReSmushIt;
    
    $result = ReSmushIt::optimize(public_path('images/example.jpg'));
    

Where to Look First

  • Facade: Golchha21\ReSmushIt\Facades\ReSmushIt for quick usage.
  • Service Class: Golchha21\ReSmushIt\ReSmushIt for custom logic.
  • Config: config/ReSmushit.php for adjusting settings.

Implementation Patterns

Core Workflows

  1. Single Image Optimization:

    $result = ReSmushIt::optimize($filePath);
    // Returns array with 'success', 'original', 'optimized', 'errors'
    
  2. Batch Processing:

    $files = [public_path('images/*.jpg')];
    $results = ReSmushIt::optimizeBatch($files);
    
  3. Queue-Based Processing (for large volumes):

    use Golchha21\ReSmushIt\Jobs\OptimizeImage;
    
    OptimizeImage::dispatch($filePath)->onQueue('optimize');
    
  4. Event Listeners (e.g., optimize on upload):

    // In EventServiceProvider
    protected $listen = [
        'eloquent.attached' => ['Golchha21\ReSmushIt\Listeners\OptimizeUploadedImage'],
    ];
    

Integration Tips

  • Storage Integration: Use Storage::disk('public')->put() to save optimized images directly to cloud storage.

    $optimizedPath = storage_path('app/optimized.jpg');
    Storage::disk('s3')->put('optimized.jpg', file_get_contents($optimizedPath));
    
  • Media Library Packages (e.g., Spatie Media Library): Extend the Analyzable trait or use events to trigger optimization post-upload.

  • API Endpoints: Create a controller to expose optimization as an API:

    public function optimize(Request $request) {
        $file = $request->file('image');
        $result = ReSmushIt::optimize($file->path());
        return response()->json($result);
    }
    
  • Artisan Command: Build a custom command for bulk optimization:

    php artisan optimize:batch /path/to/images
    

Gotchas and Tips

Pitfalls

  1. API Key Missing:

    • Error: Invalid API key or empty responses.
    • Fix: Ensure 'api_key' is set in config/ReSmushit.php.
  2. File Size Limits:

    • Error: File too large (default: 5MB).
    • Fix: Adjust 'max_filesize' in config or pre-process large files.
  3. Mime Type Restrictions:

    • Error: Unsupported mime type (e.g., .webp).
    • Fix: Add supported mime types to 'mime' array in config.
  4. Rate Limiting:

    • Error: Too many requests (reSmush.it has limits).
    • Fix: Implement retries with exponential backoff or queue delays.
  5. EXIF Data Loss:

    • Issue: Disabled by default ('exif' => false).
    • Fix: Set 'exif' => true if metadata preservation is critical.
  6. Network Issues:

    • Error: Timeouts or failed requests.
    • Fix: Use ReSmushIt::setTimeout(30) or retry logic.

Debugging

  • Enable Logging: Add 'debug' => true to config to log API responses.
  • Check Raw Response: Use ReSmushIt::getLastResponse() to inspect the raw API output.
  • Validate Input: Always check $result['success'] and $result['errors'] before processing.

Extension Points

  1. Custom API Endpoint: Override the default endpoint in config:

    'endpoint' => 'https://custom-resmush.it/api',
    
  2. Pre/Post-Processing: Extend the Golchha21\ReSmushIt\ReSmushIt class to add logic before/after optimization:

    public function optimize($filePath) {
        // Pre-processing (e.g., resize)
        $result = parent::optimize($filePath);
        // Post-processing (e.g., rename)
        return $result;
    }
    
  3. Fallback for Offline Mode: Implement a local fallback (e.g., Intervention Image) when reSmush.it is unavailable:

    if (!ReSmushIt::isOnline()) {
        return $this->fallbackOptimize($filePath);
    }
    
  4. Webhook Integration: Use reSmush.it’s webhook feature (if supported) to trigger post-optimization actions:

    // In config
    'webhook_url' => route('resmushit.webhook'),
    

Performance Tips

  • Batch Processing: Process files in chunks to avoid hitting rate limits:
    $batch = array_chunk($files, 10);
    foreach ($batch as $files) {
        ReSmushIt::optimizeBatch($files);
    }
    
  • Cache Results: Cache optimized images to avoid redundant API calls:
    $cacheKey = 'optimized_'.md5($filePath);
    if (Cache::has($cacheKey)) {
        return Cache::get($cacheKey);
    }
    $result = ReSmushIt::optimize($filePath);
    Cache::put($cacheKey, $result, now()->addHours(1));
    return $result;
    
  • Async Processing: Use Laravel Queues to offload optimization to background jobs for better responsiveness.
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.
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
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle