spatie/laravel-glide
Laravel wrapper around League Glide for easy image manipulation and caching. Create, resize, crop, apply filters, and save images via a fluent API (e.g., width, greyscale). Supports GD or Imagick, with configurable settings and a convenient facade.
Installation:
composer require spatie/laravel-glide
Publish the config file (optional):
php artisan vendor:publish --provider="Spatie\GlideLaravel\GlideServiceProvider"
First Use Case: Generate a resized image URL in a Blade template:
<img src="{{ Glide::image('path/to/image.jpg')->width(200)->url() }}">
Or programmatically:
$url = Glide::image('path/to/image.jpg')->width(200)->url();
Key Configurations:
config/glide.php for storage paths, cache settings, and response headers.glide disk (default: public) exists in config/filesystems.php.URL Generation (Most Common):
// Basic resizing
Glide::image('image.jpg')->width(300)->height(200)->url();
// With transformations
Glide::image('image.jpg')
->fit(300, 200)
->blur(10)
->text('Hello', 50, 50, 'white', 'Arial')
->url();
Direct File Output:
GlideImage::create(storage_path('app/image.jpg'))
->modify(['w' => 500, 'filt' => 'grayscale'])
->save(storage_path('app/resized.jpg'));
Cache Control:
cache() to force caching (e.g., for thumbnails):
Glide::image('image.jpg')->width(100)->cache();
Glide::image('image.jpg')->width(100)->noCache();
Middleware Integration:
Spatie\GlideLaravel\Http\Middleware\HandleGlideRequests to your $middleware array in app/Http/Kernel.php.routes/web.php:
Route::glide('images/{filename}', function () {
return Glide::image('images/' . $filename);
});
Dynamic Image Generation:
Glide::generate() for server-side processing:
Glide::generate('image.jpg', function ($image) {
$image->width(400)->height(400)->circle();
});
Custom Disk Handling: Override the default disk for specific images:
Glide::image('image.jpg')->disk('s3')->width(200)->url();
Response Headers: Customize headers per request:
Glide::image('image.jpg')->width(200)->responseHeaders(['Cache-Control' => 'public, max-age=31536000']);
Queue Processing: Offload heavy processing to queues:
Glide::image('large-image.jpg')
->width(1000)
->queue()
->url(); // Returns a URL to the queued job's result
Blade Directives: Create a custom Blade directive for reusable transformations:
Blade::directive('glide', function ($expression) {
return "<?php echo Glide::image({$expression})->width(200)->url(); ?>";
});
Usage:
<img src="{{ glide('image.jpg') }}">
Path Resolution:
glide.php (source_path, cache_path) are absolute and writable.public_path(), storage_path(), or base_path() for dynamic paths:
Glide::image(public_path('images/image.jpg'))->width(200)->url();
Cache Invalidation:
php artisan glide:clear
noCache() or unique query strings (e.g., ?t=123) for testing.Memory Limits:
->queue() or optimize with ->fit() instead of ->width()/->height().memory_limit in php.ini if needed (e.g., 2048M).File Permissions:
glide disk’s storage directory is writable:
chmod -R 755 storage/app/public/glide-cache
Route Conflicts:
/glide/*.php artisan route:list after adding middleware.Image Formats:
webp for modern browsers. Force formats with:
Glide::image('image.jpg')->format('png')->url();
Log Transformations:
Enable debug mode in glide.php:
'debug' => env('GLIDE_DEBUG', false),
Logs will appear in storage/logs/laravel.log.
Check Generated URLs: Inspect the final URL for errors (e.g., missing query params):
dd(Glide::image('image.jpg')->width(200)->toResponse()->getOriginalContent());
Validate Input:
Glide::validateImage() to check if a path is processable:
if (!Glide::validateImage('image.jpg')) {
abort(404);
}
Test Locally:
php artisan glide:test to verify setup.Cache Aggressively:
Cache-Control headers for static thumbnails:
Glide::image('thumbnail.jpg')->width(100)->cache()->responseHeaders(['Cache-Control' => 'public, max-age=31536000']);
Lazy Load:
loading="lazy" in Blade:
<img src="{{ Glide::image('image.jpg')->width(200)->url() }}" loading="lazy">
Pre-Generate Images:
long_text or S3 objects).Custom Filters: Extend Glide’s filters by creating a custom service provider:
use League\Glide\Responses\LaravelResponseFactory;
use League\Glide\Server;
class CustomGlideServiceProvider extends ServiceProvider {
public function register() {
$this->app->singleton(Server::class, function ($app) {
$server = new Server(storage_path('app'), new LaravelResponseFactory($app['request']));
$server->addFilter(new CustomFilter());
return $server;
});
}
}
Override Responses: Customize the response factory for non-Laravel apps:
Glide::setResponseFactory(new CustomResponseFactory());
Hooks: Use Laravel’s service container to inject middleware or decorators:
$this->app->extend('glide', function ($glide) {
$glide->addMiddleware(new CustomGlideMiddleware());
return $glide;
});
Queue Drivers:
Configure the queue driver for ->queue() in glide.php:
'queue' => [
'driver' => 'database',
'connection' => 'mysql',
'table' => 'glide_jobs',
],
How can I help you explore Laravel packages today?