Installation
composer require antwebes/image-resize-bundle
Add to AppKernel.php (Symfony 2.x):
new Antwebes\ImageResizeBundle\AntwebesImageResizeBundle(),
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
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");
}
Dynamic Resizing in Controllers
$resizer = $this->get('antwebes_image_resize.resizer');
$resizedPath = $resizer->resize(
$uploadedFile->getPathname(),
'thumbnail',
['quality' => 80] // Optional parameters
);
Batch Processing
$files = glob('/path/to/images/*.jpg');
foreach ($files as $file) {
$resizer->resize($file, 'medium');
}
Integration with Uploads
public function uploadAction(Request $request) {
$file = $request->files->get('image');
$resizedPath = $resizer->resize($file->getPathname(), 'thumbnail');
// Save $resizedPath to database
}
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]);
Imagine Dependency Version
imagine/imagine:0.6.*@dev. Ensure compatibility:
composer require imagine/imagine:0.6.*
File Permissions
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
No Automatic Cleanup
GD vs. Imagick
config.yml:
antwebes_image_resize:
driver: imagick
Check Resize Logs
Enable Imagine debug mode in config.yml:
antwebes_image_resize:
debug: true
Logs appear in app/logs/.
Validate Presets
config.yml:
presets:
invalid_preset:
width: 0 # Invalid: width/height must be > 0
Handle Unsupported Formats
try {
$resizer->resize($file, 'thumbnail');
} catch (\Imagine\Exception\InvalidArgumentException $e) {
// Handle unsupported format
}
Custom Filters Extend the resizer with Imagine filters:
$resizer->resize($file, null, [
'filters' => [
new \Imagine\Filter\Colorize\Grayscale(),
new \Imagine\Filter\Strip(),
]
]);
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 }
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'
How can I help you explore Laravel packages today?