dahromy/glide-symfony
Symfony bundle integrating League Glide for on-the-fly image resizing and transformations. Configure source/cache, choose GD or Imagick, define defaults and presets, and generate signed image URLs via Twig functions/filters or the GlideService for controller responses.
composer require dahromy/glide-symfony
config/bundles.php:
DahRomy\GlideBundle\DahRomyGlideBundle::class => ['all' => true],
config/packages/glide.yaml:
glide:
source: '%kernel.project_dir%/public/uploads/images'
cache: '%kernel.project_dir%/var/cache/glide'
driver: gd
<img src="{{ 'profile.jpg'|glide({w: 200, h: 200, fit: 'crop'}) }}" alt="Profile">
glide_asset() and |glide filter for quick template integration.GlideService for programmatic URL generation or direct responses.glide.yaml for reusable image transformations.Dynamic Image Delivery Use Twig filters for frontend:
{{ asset('images/' ~ product.image) | glide({w: 800, h: 600, fit: 'contain'}) }}
Or in controllers:
$url = $glideService->getImageUrl("images/{$product->image}", ['w' => 800]);
return $this->redirect($url);
Preset-Based Transformations
Define presets in glide.yaml:
presets:
thumbnail: {w: 150, h: 150, fit: 'crop'}
featured: {w: 1200, h: 800, fit: 'contain'}
Apply in Twig:
{{ 'product.jpg'|glide({}, 'thumbnail') }}
Direct Image Responses Stream images without URL generation (e.g., for APIs):
return $glideService->getImageResponse('image.jpg', ['w' => 500]);
Signed URLs for Security Enable signing in config:
glide:
signing: true
secret: '%env(APP_SECRET)%'
Use signed URLs in Twig:
{{ 'private.jpg'|glide({w: 300}, null, true) }} {# 3rd arg: signed #}
public/uploads/images (configurable).php bin/console cache:clear --env=prod
Or programmatically:
$glideService->clearCache();
// stimulus_controller.js
connect() {
this.element.addEventListener('resize', () => {
this.element.src = this.element.src.replace(/w=\d+/, `w=${this.element.width}`);
});
}
Driver Dependencies
gd or imagick is installed (php -m | grep gd or php -m | grep imagick).gd if imagick fails (configured in glide.yaml).Path Resolution
source. Use public/ or var/ paths./images/ vs images/ inconsistencies in config.Cache Invalidation
$glideService->clearCache(); // Clears entire cache directory
Preset Overrides
{{ 'image.jpg'|glide({w: 100}, 'thumbnail') }} {# w=100 overrides preset #}
?debug=1 to see raw Glide params:
/cache/glide/abc123.jpg?w=200&h=200&fit=crop&debug=1
driver: gd for consistency across environments.Custom Drivers
Extend GlideService to support additional drivers (e.g., vips):
// src/Service/CustomGlideService.php
class CustomGlideService extends GlideService {
protected function getDriver(): string { return 'vips'; }
}
Register as a service alias.
Event Listeners Hook into Glide events (e.g., cache miss):
// config/services.yaml
DahRomy\GlideBundle\EventListener\GlideListener:
tags:
- { name: kernel.event_listener, event: glide.cache_miss, method: onCacheMiss }
Dynamic Configs Override presets via runtime logic:
$glideService->setPreset('dynamic', ['w' => $request->query->getInt('width')]);
cache outside public/ (e.g., var/cache/glide) to avoid web access.How can I help you explore Laravel packages today?