ralphjsmit/laravel-glide
Generate responsive srcset/sizes image URLs on the fly with Glide in Laravel. Drop in the original image, use glide()->src() in Blade, and get resized variants served automatically (with lazy loading), no manual exports or config needed.
Installation: Add the package via Composer:
composer require ralphjsmit/laravel-glide
No additional configuration is required for basic usage.
First Use Case: Replace static image tags in Blade templates with the glide()->src() helper. For example:
<img {{ glide()->src('path/to/image.jpg') }} alt="Description">
This automatically generates a responsive <img> tag with srcset and loading="lazy" attributes.
Verify Output: Inspect the rendered HTML to confirm the srcset includes dynamically generated URLs for different widths (e.g., ?width=400, ?width=800).
Responsive Image Generation:
glide()->src('path/to/image.jpg', maxWidth) to generate a responsive image with a maximum width constraint.<img {{ glide()->src('hero.jpg', 1200) }} alt="Hero Image">
srcset with widths up to the specified maxWidth (e.g., 400w, 800w, 1200w).Custom sizes Attribute:
sizes attribute to hint browsers about image dimensions at different viewports.<img {{ glide()->src('product.jpg', 800, sizes: '(max-width: 768px) 100vw, 50vw') }} alt="Product">
Lazy Loading:
loading="lazy"):
<img {{ glide()->src('logo.png', lazy: false) }} alt="Logo">
SVG Handling:
srcset generation (avoids unnecessary processing).URL Scoping:
'domain' => env('GLIDE_DOMAIN', null),
Dynamic Paths:
asset() or model paths (e.g., glide()->src($product->imagePath)) for dynamic image sources.Caching:
storage/framework/cache/glide). Clear cache manually or via:
php artisan glide:clear
Custom Configurations:
php artisan vendor:publish --tag=glide-config
config/glide.php to adjust:
scales: Custom width array (e.g., [300, 600, 1200]).grow: Allow images to exceed original dimensions (default: true).upscale: Disable upscaling (default: true).Component Integration:
@component('components.responsive-image', ['path' => 'banner.jpg', 'maxWidth' => 1600])
@endcomponent
Testing:
$response = $this->get('/glide/path/to/image.jpg?width=400');
$response->assertStatus(200);
Cache Invalidation:
php artisan glide:clear after editing images or automate it in deployment pipelines.SVG Misconfiguration:
srcset attributes unnecessarily, which can bloat HTML.srcset for SVGs by default (no action required).URL Encoding:
Upscaling Behavior:
'upscale' => false,
grow:
'grow' => false,
Domain Scoping:
domain config is misconfigured (e.g., pointing to a non-existent CDN).config/glide.php and ensure the domain is reachable.Disk Support:
'disk' => 's3',
config/filesystems.php.Inspect Generated HTML:
srcset and sizes attributes. Check for missing or malformed URLs.Check Cache Directory:
storage/framework/cache/glide. Empty the directory if stale images persist.Glide Logs:
config/glide.php:
'debug' => true,
storage/logs/laravel.log) for Glide-related errors.Test with curl:
curl "https://your-app.com/glide/path/to/image.jpg?width=800"
200 OK responses and correct image dimensions.Disable Caching Temporarily:
'cache' => false,
Custom Scales:
'scales' => [200, 400, 800, 1200, 1600],
Image Processing:
<img {{ glide()->src('image.jpg', 800, format: 'webp', blur: 10) }} alt="Blurred Preview">
format, blur, sharpen, resize, etc.Middleware Integration:
Route::middleware(['glide'])->group(function () {
// Glide routes
});
Event Listeners:
Glide::listen('cache.miss', function ($path) {
Log::info("Generated image: {$path}");
});
Custom Directives:
Blade::directive('glideImage', function ($expression) {
return "<?php echo glide()->src({$expression}); ?>";
});
Usage:
<img @glideImage("path/to/image.jpg") alt="Dynamic Image">
How can I help you explore Laravel packages today?