Installation:
composer require creavio/image-manage-bundle
Add to config/bundles.php:
return [
// ...
Creavio\ImageManageBundle\CreavioImageManageBundle::class => ['all' => true],
];
Configuration: Publish the default config:
php bin/console creavio:image-manage:install
Edit config/packages/creavio_image_manage.yaml to define:
image_directories (source paths)resize_configs (named presets like thumbnail, large)First Use Case: In a Twig template, reference a resized image:
<img src="{{ asset('images/product.jpg|resize:thumbnail') }}" alt="Product">
|resize:thumbnail filter applies the thumbnail preset from config.Define Presets:
Configure resizing rules in config/packages/creavio_image_manage.yaml:
resize_configs:
thumbnail:
width: 200
height: 200
mode: 'outbound' # or 'inbound', 'exact'
large:
width: 800
height: null
quality: 80
Twig Integration:
|resize filter in Twig:
{{ asset('path/to/image.jpg|resize:large') }}
|crop if supported):
{{ asset('path/to/image.jpg|resize:thumbnail|crop:50x50') }}
Dynamic Presets: Pass runtime values via Twig:
{% set customSize = '400x300' %}
{{ asset('image.jpg|resize:custom:' ~ customSize) }}
Define in config:
resize_configs:
custom: { width: null, height: null } # Overridden by runtime args
Asset Pipeline:
?v={{ filemtime('path/to/image.jpg') }} to URLs.asset() for automatic versioning:
{{ asset('image.jpg|resize:thumbnail', {'fallback': 'default.jpg'}) }}
Controller Integration: Generate URLs programmatically:
use Creavio\ImageManageBundle\Twig\ResizeExtension;
$resizeUrl = $this->get('twig')->getExtension(ResizeExtension::class)
->getResizeUrl('/uploads/image.jpg', 'thumbnail');
Conditional Resizing: Use Twig logic to switch presets:
{% set preset = isMobile ? 'mobile' : 'desktop' %}
{{ asset('image.jpg|resize:' ~ preset) }}
Fallbacks: Handle missing images:
{{ asset('image.jpg|resize:thumbnail', {'fallback': 'no-image.png'}) }}
Batch Processing: Use the CLI command to pre-generate resized versions:
php bin/console creavio:image-manage:resize --dir=/path/to/images --preset=thumbnail
Caching Issues:
file_put_contents in a post-resize event to save to disk.php bin/console cache:clear
Path Configuration:
image_directories must include the full server path (e.g., /var/www/html/public/uploads), not a web URL.Twig Filter Scope:
|resize filter only works with asset(). Direct URLs (e.g., {{ imageUrl|resize }}) will fail.{# ❌ Won't work #}
<img src="{{ imageUrl|resize:thumbnail }}">
Quality Settings:
quality: 75 may produce large files. Adjust in config:
resize_configs:
high_quality: { quality: 90 }
File Permissions:
www-data) has write permissions to the output directory (if saving resized files).Check Config:
Validate resize_configs syntax. Use:
php bin/console debug:config creavio_image_manage
Log Errors:
Enable debug mode and check var/log/dev.log for:
Test CLI Commands: Manually trigger resizing to verify:
php bin/console creavio:image-manage:resize --dir=/path/to/image.jpg --preset=thumbnail
Fallback Behavior:
fallback is not set, missing images return a 404. Always specify a fallback in production:
{{ asset('image.jpg|resize:thumbnail', {'fallback': 'fallback.jpg'}) }}
Custom Filters: Extend the bundle by adding new Twig filters in a service:
# config/services.yaml
services:
App\Twig\CustomImageExtension:
tags: ['twig.extension']
Example extension:
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class CustomImageExtension extends AbstractExtension {
public function getFunctions() {
return [
new TwigFunction('custom_resize', [$this, 'customResize']),
];
}
public function customResize($path, $preset, $options = []) {
// Integrate with ImageManageBundle's logic
}
}
Event Listeners: Hook into the resize process to add watermarks or metadata:
namespace App\EventListener;
use Creavio\ImageManageBundle\Event\ResizeEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class WatermarkSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
ResizeEvent::NAME => 'onResize',
];
}
public function onResize(ResizeEvent $event) {
$event->getImage()->addWatermark('watermark.png');
}
}
Override Templates: Customize the Twig template for resize URLs by copying:
vendor/creavio/image-manage-bundle/Resources/views/
to your project’s templates/creavio_image_manage/ directory.
mode: 'exact': Preserves aspect ratio without padding (faster than outbound).How can I help you explore Laravel packages today?