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

Resize Laravel Package

didweb/resize

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation Run:

    composer require didweb/resize:1.*
    

    Ensure Didweb\Bundle\ResizeBundle\DidwebResizeBundle() is registered in app/AppKernel.php under $bundles.

  2. Configuration Add the service import to app/config/config.yml:

    imports:
        - { resource: "@DidwebResizeBundle/Resources/config/services.yml" }
    

    Define resize parameters in app/config/config.yml:

    didweb_resize:
        img_ancho_p: 240    # Small width (px)
        img_alto_p: 196     # Small height (px)
        img_ancho_g: 1024   # Large width (px)
        img_alto_g: 768     # Large height (px)
        img_directorio: "%kernel.root_dir%/../web/fotos"  # Output directory
    
  3. First Use Case Use the ResizeService to resize an image:

    use Didweb\Bundle\ResizeBundle\Service\ResizeService;
    
    $resizeService = $this->get('didweb_resize.service');
    $resizeService->resize('path/to/original/image.jpg');
    

    This generates two versions (small and large) in the configured directory.


Implementation Patterns

Core Workflows

  1. Resizing Images Inject ResizeService into controllers/services:

    public function uploadAction(Request $request) {
        $file = $request->files->get('image');
        $resizeService = $this->get('didweb_resize.service');
        $resizeService->resize($file->getPathname());
    }
    
  2. Dynamic Configuration Override defaults via dependency injection:

    # app/config/config.yml
    services:
        didweb_resize.service:
            arguments:
                - { img_ancho_p: 300, img_alto_p: 200 }  # Override small size
    
  3. Batch Processing Loop through uploaded files:

    foreach ($request->files->get('images') as $file) {
        $resizeService->resize($file->getPathname());
    }
    
  4. Integration with Symfony Forms Use with FileType fields:

    $builder->add('image', FileType::class, [
        'mapped' => false,
        'required' => false,
    ]);
    

Advanced Patterns

  • Custom Filenames Extend the service to append metadata (e.g., timestamps):

    $resizeService->resize($path, 'custom_prefix_');
    
  • Event Listeners Trigger post-resize actions (e.g., database updates):

    $eventDispatcher->addListener(
        'didweb.resize.after',
        function ($event) { /* Handle post-resize logic */ }
    );
    

Gotchas and Tips

Pitfalls

  1. Directory Permissions Ensure img_directorio is writable by the web server (e.g., chmod -R 775 web/fotos).

  2. Image Format Preservation The package does not auto-convert formats. Ensure input/output paths use the same extension (e.g., .jpg.jpg).

  3. Aspect Ratio Distortion Hardcoded dimensions may distort images. Use maintainAspectRatio (if supported) or implement dynamic ratios:

    didweb_resize:
        maintain_ratio: true  # Hypothetical config (not natively supported)
    
  4. Memory Limits Large images may hit PHP’s memory_limit. Increase if needed:

    ini_set('memory_limit', '256M');
    

Debugging

  • Verify Config Check app/config/config.yml for typos in keys (e.g., img_ancho_p vs. img_ancho_P).

  • Log Errors Wrap calls in try-catch:

    try {
        $resizeService->resize($path);
    } catch (\Exception $e) {
        $this->container->get('logger')->error($e->getMessage());
    }
    
  • Test with Known Files Use a small test image (e.g., 100x100px) to validate the pipeline.

Extension Points

  1. Custom Resize Logic Override the service class:

    class CustomResizeService extends \Didweb\Bundle\ResizeBundle\Service\ResizeService {
        protected function getSmallDimensions() {
            return [400, 300]; // Custom small size
        }
    }
    

    Register it in services.yml:

    didweb_resize.service: '@custom_resize_service'
    
  2. Add Watermarks Extend the service to apply watermarks post-resize using libraries like Imagine.

  3. Support Additional Formats Modify the underlying Imagine bundle configuration to handle formats like WebP.

Tips

  • Use Environment Variables Externalize paths/configs for deployments:

    img_directorio: "%env(RESIZE_DIR)%"
    
  • Cache Resized Images Implement a cache layer (e.g., Redis) to avoid reprocessing identical files.

  • Document Output Structure Standardize filenames (e.g., original_123.jpgsmall_123.jpg, large_123.jpg) for consistency.

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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky