league/glide-symfony
Symfony adapter for League Glide that plugs Glide image manipulation and on-the-fly resizing into Symfony apps. Install via Composer and follow the core Glide docs for setup, caching, and URL-based transformations.
Installation
composer require league/glide-symfony league/glide
Ensure your composer.json includes Symfony 5.4+ and PHP 8.0+ constraints.
Enable the Bundle
Add to config/bundles.php:
return [
// ...
League\Glide\Symfony\GlideBundle::class => ['all' => true],
];
Basic Configuration
Create config/packages/glide.yaml:
glide:
source: '%kernel.project_dir%/public/uploads' # Local path or S3 bucket
cache: '%kernel.cache_dir%/glide' # Cache directory
drivers:
local:
adapter: 'local'
directory: '%kernel.project_dir%/public/uploads'
http_driver: 'local' # or 's3', 'rackspace', etc.
Route the Endpoint
Add to config/routes.yaml:
glide:
resource: "@GlideSymfonyBundle/Resources/config/routing.yaml"
prefix: /glide
First Use Case: Dynamic Thumbnails Generate a URL in a controller:
use League\Glide\Symfony\Glide;
public function showProduct(Glide $glide, string $productId): Response
{
$imagePath = "products/{$productId}/image.jpg";
$url = $glide->url($imagePath, [
'width' => 300,
'height' => 300,
'fit' => 'crop',
'format' => 'webp'
]);
return $this->render('product/show.html.twig', ['imageUrl' => $url]);
}
Use in Twig:
<img src="{{ imageUrl }}" alt="Product">
VichUploaderBundle or manual upload.Example with VichUploaderBundle:
// Controller
public function upload(UploadHandler $handler): Response
{
$handler->setUploadRoot($this->getParameter('kernel.project_dir').'/public/uploads');
// ...
}
// Twig
<img src="{{ path(handler.result.imageUrl, {
'width': 200,
'height': 200,
'fit': 'crop'
}) }}">
Use Twig extensions or controller logic to generate multiple variants:
{# srcset for responsive images #}
<img src="{{ imageUrl|glide({width: 800}) }}"
srcset="{{ imageUrl|glide({width: 400}) }} 400w,
{{ imageUrl|glide({width: 800}) }} 800w"
sizes="(max-width: 600px) 400px, 800px">
Configure S3 in glide.yaml:
glide:
drivers:
s3:
adapter: 'aws'
key: '%env(AWS_ACCESS_KEY_ID)%'
secret: '%env(AWS_SECRET_ACCESS_KEY)%'
bucket: 'my-app-images'
region: 'us-east-1'
http_driver: 's3'
glide:
cache: '%kernel.cache_dir%/glide'
Clear cache via:
php bin/console cache:clear
$response->setPublic();
$response->setMaxAge(3600); // Cache for 1 hour
Twig Integration: Create a custom Twig extension for reusable filters:
// src/Twig/GlideExtension.php
class GlideExtension extends \Twig\Extension\AbstractExtension
{
public function getFunctions()
{
return [
new \Twig\TwigFunction('glide_url', [$this->glide, 'url']),
];
}
}
Register in services.yaml:
services:
App\Twig\GlideExtension:
tags: ['twig.extension']
ApiPlatform: Use Glide URLs in serializers:
use League\Glide\Symfony\Glide;
class ProductOutput
{
public function __construct(private Glide $glide) {}
public function __invoke(Product $product, Operation $operation, array $context): array
{
return [
'image' => $this->glide->url($product->getImagePath(), ['width' => 200]),
];
}
}
Doctrine ORM: Store Glide URLs in DB for reuse:
// Entity
class Product
{
#[ORM\Column(type: 'string', nullable: true)]
private ?string $glideThumbnailUrl;
public function setGlideThumbnailUrl(Glide $glide): self
{
$this->glideThumbnailUrl = $glide->url($this->imagePath, ['width' => 150]);
return $this;
}
}
loading="lazy" in Twig:
<img src="{{ imageUrl }}" loading="lazy" alt="...">
$url = $glide->url($imagePath, [
'width' => 300,
'preset' => 'private',
'expires' => time() + 3600 // 1 hour
]);
/glide with middleware:
// src/EventListener/GlideSecurityListener.php
class GlideSecurityListener
{
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();
if ($request->getPathInfo() === '/glide') {
if (!$request->isXmlHttpRequest() && !$request->headers->has('X-API-Key')) {
$event->setResponse(new Response('Forbidden', 403));
}
}
}
}
Register in services.yaml:
services:
App\EventListener\GlideSecurityListener:
tags: ['kernel.event_listener', { event: 'kernel.request', method: 'onKernelRequest' }]
Storage Paths:
source path in glide.yaml causes 404 errors.%kernel.project_dir%/public/uploads) and verify permissions (chmod -R 755).Cache Invalidation:
php bin/console cache:pool:clear glide
S3 Permissions:
glide.yaml:
glide:
debug: true
Check Symfony logs for AWS Error.Memory Limits:
memory_limit.php.ini or optimize Glide filters (e.g., avoid redundant resizing).URL Generation:
glide->url() returns malformed URLs.http_driver matches your storage backend (e.g., s3 for AWS).Symfony Cache Conflicts:
glide:
cache: '%kernel.cache_dir%/glide'
framework:
cache:
pools:
glide: ~ # Disable Symfony cache for Glide
glide:
debug: true
Logs detailedHow can I help you explore Laravel packages today?