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

Image Resize Bundle Laravel Package

antwebes/image-resize-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require antwebes/image-resize-bundle
    

    Add to AppKernel.php (Symfony 2.x):

    new Antwebes\ImageResizeBundle\AntwebesImageResizeBundle(),
    
  2. Configuration Define resize presets in config.yml:

    antwebes_image_resize:
        presets:
            thumbnail:
                width: 150
                height: 150
                mode: outbound
            medium:
                width: 600
                height: null
                mode: outbound
    
  3. First Use Case Resize an image in a controller:

    use Antwebes\ImageResizeBundle\ImageResize;
    
    public function resizeAction($filePath, $preset) {
        $resizer = $this->get('antwebes_image_resize.resizer');
        $resizedPath = $resizer->resize($filePath, $preset);
        return new Response("Resized image saved to: $resizedPath");
    }
    

Implementation Patterns

Common Workflows

  1. Dynamic Resizing in Controllers

    $resizer = $this->get('antwebes_image_resize.resizer');
    $resizedPath = $resizer->resize(
        $uploadedFile->getPathname(),
        'thumbnail',
        ['quality' => 80] // Optional parameters
    );
    
  2. Batch Processing

    $files = glob('/path/to/images/*.jpg');
    foreach ($files as $file) {
        $resizer->resize($file, 'medium');
    }
    
  3. Integration with Uploads

    public function uploadAction(Request $request) {
        $file = $request->files->get('image');
        $resizedPath = $resizer->resize($file->getPathname(), 'thumbnail');
        // Save $resizedPath to database
    }
    
  4. Custom Presets via Services Define dynamic presets in a service:

    services:
        app.image_resizer:
            class: Antwebes\ImageResizeBundle\ImageResize
            arguments:
                - '@antwebes_image_resize.resizer'
                - '@service_container'
    
    $resizer->resize($file, null, ['width' => 800, 'height' => 600]);
    

Gotchas and Tips

Pitfalls

  1. Imagine Dependency Version

    • The bundle requires imagine/imagine:0.6.*@dev. Ensure compatibility:
      composer require imagine/imagine:0.6.*
      
    • Upgrading Imagine may break the bundle.
  2. File Permissions

    • Resized images are saved in app/cache/antwebes_image_resize/. Ensure the directory is writable:
      mkdir -p app/cache/antwebes_image_resize
      chmod -R 777 app/cache/antwebes_image_resize
      
  3. No Automatic Cleanup

    • The bundle does not delete original files or old resized versions. Implement a cleanup strategy (e.g., cron job) if needed.
  4. GD vs. Imagick

    • The bundle uses Imagine, which defaults to GD. For better quality, configure Imagick in config.yml:
      antwebes_image_resize:
          driver: imagick
      

Debugging Tips

  1. Check Resize Logs Enable Imagine debug mode in config.yml:

    antwebes_image_resize:
        debug: true
    

    Logs appear in app/logs/.

  2. Validate Presets

    • If a preset fails, verify its configuration in config.yml:
      presets:
          invalid_preset:
              width: 0  # Invalid: width/height must be > 0
      
  3. Handle Unsupported Formats

    • The bundle relies on Imagine’s supported formats (e.g., JPEG, PNG, GIF). Unsupported formats throw exceptions:
      try {
          $resizer->resize($file, 'thumbnail');
      } catch (\Imagine\Exception\InvalidArgumentException $e) {
          // Handle unsupported format
      }
      

Extension Points

  1. Custom Filters Extend the resizer with Imagine filters:

    $resizer->resize($file, null, [
        'filters' => [
            new \Imagine\Filter\Colorize\Grayscale(),
            new \Imagine\Filter\Strip(),
        ]
    ]);
    
  2. Event Listeners Subscribe to resize events (if the bundle supports them; check source for Events class):

    services:
        app.image_resize_listener:
            class: AppBundle\EventListener\ImageResizeListener
            tags:
                - { name: kernel.event_listener, event: antwebes.image_resize, method: onResize }
    
  3. Override Resizer Service Replace the default resizer with a custom implementation:

    services:
        antwebes_image_resize.resizer:
            class: AppBundle\Service\CustomImageResizer
            arguments:
                - '@antwebes_image_resize.imagine'
    
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.
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
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours