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

Laravel Image Resizer Laravel Package

ab01faz101/laravel-image-resizer

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require ab01faz101/laravel-image-resizer
    

    Publish the config file (if needed):

    php artisan vendor:publish --provider="Ab01faz101\LaravelImageResizer\ImageResizerServiceProvider"
    
  2. Basic Usage Resize an image stored in storage to predefined sizes:

    use Ab01faz101\LaravelImageResizer\Facades\ImageResizer;
    
    $path = 'images/original.jpg';
    $sizes = ['small', 'medium', 'large']; // Defined in config
    
    ImageResizer::resize($path, $sizes);
    
  3. First Use Case

    • Upload an image via a form, then resize it for a blog post thumbnail and featured image:
    $request->file('image')->store('uploads');
    $path = $request->file('image')->hashName();
    ImageResizer::resize($path, ['thumbnail', 'featured']);
    

Implementation Patterns

Common Workflows

  1. Dynamic Size Generation Define custom sizes in config/laravel-image-resizer.php:

    'sizes' => [
        'thumbnail' => ['width' => 150, 'height' => 150, 'fit' => 'crop'],
        'featured'  => ['width' => 800, 'height' => 450, 'fit' => 'contain'],
    ],
    

    Use them in code:

    ImageResizer::resize($path, ['thumbnail', 'featured']);
    
  2. Integration with Eloquent Add a resizeImages() method to your model:

    public function resizeImages()
    {
        $this->update([
            'thumbnail' => ImageResizer::resize($this->image_path, ['thumbnail']),
            'featured'  => ImageResizer::resize($this->image_path, ['featured']),
        ]);
    }
    
  3. Batch Processing Resize all images in a directory:

    $files = Storage::files('uploads');
    foreach ($files as $file) {
        ImageResizer::resize(basename($file), ['small', 'medium']);
    }
    
  4. Queueing for Large Files Dispatch a job to avoid timeouts:

    ResizeImageJob::dispatch($path, ['large']);
    

Integration Tips

  • Storage Disk: Ensure the default disk in config/filesystems.php is writable.
  • Cache Busting: Append a timestamp to output paths if caching is enabled:
    ImageResizer::resize($path, ['small'], ['cache' => false]);
    
  • Intervention Image: The package uses Intervention under the hood. Leverage its features (e.g., filters) via:
    ImageResizer::resize($path, ['small'], ['filters' => function ($image) {
        return $image->filters()->sepia();
    }]);
    

Gotchas and Tips

Pitfalls

  1. File Permissions

    • Ensure the storage directory has 755 permissions. Silent failures may occur if the package can’t write resized images.
  2. Missing Config

    • If config/laravel-image-resizer.php is missing, the package falls back to defaults but may throw errors for undefined sizes.
  3. Image Validation

    • The package doesn’t validate input files. Always validate is_image() and mimes('image/*') in your controller.
  4. Overwriting Files

    • By default, resized images overwrite existing files. Use a unique suffix (e.g., timestamp) to avoid conflicts:
      ImageResizer::resize($path, ['small'], ['suffix' => '_resized']);
      

Debugging

  • Log Output Paths Enable debug mode to log paths:
    ImageResizer::setDebug(true);
    
  • Check Disk Space Large resizes may fail silently due to disk space. Monitor storage usage.

Extension Points

  1. Custom Resize Logic Extend the ResizeImage class to add pre/post-processing:

    ImageResizer::extend('custom', function ($path, $options) {
        // Custom logic here
        return $resizedPath;
    });
    
  2. Event Listeners Trigger events for resizing:

    ImageResizer::resize($path, ['small'])
        ->then(function ($result) {
            // Post-resize logic
        });
    
  3. Fallback for Unsupported Formats Add a fallback for non-JPEG/PNG images (e.g., SVG):

    ImageResizer::resize($path, ['small'], ['fallback' => 'default.svg']);
    

Performance

  • Disable Cache for Development Set 'cache' => false in config to avoid stale resized images during development.
  • Use fit Wisely fit: 'crop' may degrade quality. Prefer contain for better results when possible.
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.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi