aferrandini/image-bundle
Laravel bundle for handling images: upload, resize, crop, cache and optimize with a simple configuration-driven workflow. Includes storage integration and helper utilities for generating thumbnails and responsive variants in your app.
Installation
Add the bundle to your composer.json:
composer require aferrandini/image-bundle
Register it in config/bundles.php:
return [
// ...
Aferrandini\ImageBundle\AferrandiniImageBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console aferrandini:image:install
Update config/packages/aferrandini_image.yaml to define:
default_driver (e.g., gd, imagick).storage (e.g., local, s3).directories (input/output paths).First Use Case Resize an image in a controller:
use Aferrandini\ImageBundle\Manager\ImageManager;
public function resize(ImageManager $imageManager)
{
$image = $imageManager->read('path/to/input.jpg');
$image->resize(300, 200);
$imageManager->save($image, 'path/to/output.jpg');
}
Dynamic Resizing Use named presets (defined in config) for reusable transformations:
# config/packages/aferrandini_image.yaml
aferrandini_image:
presets:
thumbnail: { width: 150, height: 150, mode: 'outbound' }
Apply in code:
$image->applyPreset('thumbnail');
Batch Processing Loop through files in a directory:
$files = glob('uploads/*.jpg');
foreach ($files as $file) {
$image = $imageManager->read($file);
$image->resize(800, null); // Auto-height
$imageManager->save($image, "resized/{$image->getFilename()}");
}
Integration with Symfony Forms
Use Aferrandini\ImageBundle\Form\Type\ImageType for file uploads:
$builder->add('image', ImageType::class, [
'label' => 'Profile Picture',
'required' => false,
'preset' => 'thumbnail', // Optional preset
]);
Aferrandini\ImageBundle\Driver\DriverInterface for cloud storage (e.g., S3).image.pre_save/image.post_save events for logging/validation.{{ image('path/to/file.jpg', 'thumbnail') }} in templates (requires AferrandiniImageTwigExtension).Driver Compatibility
imagick requires PHP Imagick extension (not gd).gd in config if imagick fails:
aferrandini_image:
drivers:
imagick: ~
gd: { fallback: true }
File Permissions
Ensure output directories (e.g., public/uploads/resized) are writable:
chmod -R 775 var/cache var/log public/uploads
Memory Limits
Large images may hit PHP’s memory_limit. Increase in php.ini or use imagick (more efficient):
memory_limit = 256M
var/log/dev.log for driver failures.php bin/console debug:config aferrandini_image
local storage first before deploying to cloud.Custom Filters
Add filters (e.g., watermark) by extending Aferrandini\ImageBundle\Filter\FilterInterface:
class WatermarkFilter implements FilterInterface {
public function apply(ImageInterface $image) { /* ... */ }
}
Register in services.yaml:
services:
App\Filter\WatermarkFilter:
tags: { name: aferrandini_image.filter, alias: 'watermark' }
Override Templates
Copy vendor/aferrandini/image-bundle/Resources/views/ to templates/aferrandini_image/ for custom Twig templates.
Command-Line Tools
Use php bin/console aferrandini:image:resize for CLI batch processing:
php bin/console aferrandini:image:resize input.jpg output.jpg 300x200
How can I help you explore Laravel packages today?