Installation Add the bundle via Composer:
composer require braune-digital/imagine-bundle
Register the bundle in config/bundles.php (Symfony):
return [
// ...
BrauneDigital\ImagineBundle\BrauneDigitalImagineBundle::class => ['all' => true],
];
Configuration Publish the default config (if needed):
php bin/console braune-digital:imagine:config:dump
Configure config/packages/braune_digital_imagine.yaml:
braune_digital_imagine:
driver: 'gd' # or 'imagick'
cache: 'cache.adapter.system'
First Use Case Use the service to resize an image in a controller:
use BrauneDigital\ImagineBundle\Service\ImagineService;
class ImageController extends AbstractController
{
public function resize(ImageService $imagineService)
{
$path = $this->getParameter('kernel.project_dir') . '/public/uploads/image.jpg';
$imagine = $imagineService->load($path);
$imagine->resize(new \Imagine\Gd\Box(200, 200));
return $this->file($imagine->get('jpg'));
}
}
Dynamic Image Processing Use the service in controllers or services to generate thumbnails on-the-fly:
$imagine = $imagineService->load($filePath)
->resize(new \Imagine\Gd\Box(300, 300))
->watermark($watermarkPath, 0.5, 0.5)
->save($outputPath);
Batch Processing Process multiple images in a loop (e.g., for bulk uploads):
foreach ($uploadedFiles as $file) {
$imagineService->load($file->getPathname())
->resize(new \Imagine\Gd\Box(800, 600))
->save($file->getPathname());
}
Integration with Symfony Forms
Use with VichUploaderBundle or custom file uploads:
$form = $this->createForm(ImageUploadType::class, $entity);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$imagineService->load($entity->getImagePath())
->resize(new \Imagine\Gd\Box(1024, 768))
->save($entity->getImagePath());
}
Caching Processed Images Leverage Symfony’s cache system to avoid reprocessing:
braune_digital_imagine:
cache: 'cache.adapter.redis'
thumbnailPath).public function onUpload(FileUploadEvent $event)
{
$imagineService->load($event->getFile()->getPathname())
->resize(new \Imagine\Gd\Box(400, 400))
->save($event->getFile()->getPathname());
}
return new BinaryFileResponse($imagine->get('png'), 200, [
'Content-Type' => 'image/png',
'Content-Disposition' => 'inline; filename="thumbnail.png"',
]);
Driver Dependencies
php-gd or php-imagick).braune_digital_imagine.yaml:
driver: 'gd'
fallback_driver: 'imagick'
File Permissions
chmod -R 775 /path/to/public/uploads
Memory Limits
memory_limit. Adjust in php.ini or use chunked processing:
$imagine->resize(new \Imagine\Gd\Box(2000, 2000))->save($outputPath, ['quality' => 80]);
Cache Invalidation
php bin/console cache:clear
braune_digital_imagine:
debug: true
$imagine = $imagineService->load($path);
if (!$imagine->valid()) {
throw new \RuntimeException('Invalid image file');
}
Custom Filters Extend the service to add reusable filters:
$imagine->customFilter(function ($image) {
$image->rotate(90);
return $image;
});
Event Dispatching Dispatch events before/after processing:
$dispatcher->dispatch(new ImageProcessEvent($imagine, 'pre_process'));
$imagine->resize(...);
$dispatcher->dispatch(new ImageProcessEvent($imagine, 'post_process'));
Configuration Overrides
Override settings per environment (e.g., config/packages/dev/braune_digital_imagine.yaml):
braune_digital_imagine:
driver: 'imagick'
debug: true
Imagine\Image\Box for Aspect Ratio:
$imagine->resize(new \Imagine\Image\Box(800, null)); // Maintain aspect ratio
$imagine->resize(new \Imagine\Gd\Box(1200, 800))
->save($outputPath, ['quality' => 75]);
config/services.yaml:
parameters:
app.upload_dir: '%kernel.project_dir%/public/uploads'
How can I help you explore Laravel packages today?