league/glide
Glide is an on-demand PHP image manipulation library with an HTTP API. Resize, crop, and apply effects, then cache results with far-future headers. Works with GD, Imagick, or libvips, integrates with Flysystem, and can sign URLs for security.
Installation:
composer require league/glide
Add to composer.json if using Laravel’s require or require-dev as needed.
Basic Server Initialization:
use League\Glide\ServerFactory;
$server = ServerFactory::create([
'source' => storage_path('app/public/images'), // Original images
'cache' => storage_path('app/public/cache'), // Processed images
]);
First Use Case: In a Laravel route or controller:
Route::get('/images/{path}', function ($path) use ($server) {
return $server->outputImage($path, $_GET);
});
Access via URL: /images/photo.jpg?w=300&h=300&fit=crop
Dynamic Image Serving:
$server->outputImage($path, $query) to handle requests dynamically.Framework Integration:
/images/* to Glide:
Route::get('/images/{path}', function ($path) {
$server = app(ServerFactory::class)->create();
return $server->outputImage($path, request()->query());
});
Server into controllers or use a GlideListener for PSR-7 responses.Caching Strategy:
Cache::setCacheTTL() (default: 1 year).URL Generation:
$url = $server->getImageUrl('photo.jpg', ['w' => 300], 3600); // Expires in 1 hour
Custom Manipulators:
Extend League\Glide\Api\Manipulator\AbstractManipulator to add new effects (e.g., watermarks):
class WatermarkManipulator extends AbstractManipulator {
public function __invoke($image, $params) {
$image->insert('watermark.png', 'bottom-right');
}
}
Register in ServerFactory:
$server = ServerFactory::create([
'source' => '...',
'cache' => '...',
'manipulators' => [new WatermarkManipulator()],
]);
Multi-Environment Config: Use Laravel’s config system to switch sources/caches per environment:
$server = ServerFactory::create([
'source' => config('glide.source'),
'cache' => config('glide.cache'),
]);
Define in config/glide.php:
'source' => env('GLIDE_SOURCE', storage_path('app/public/images')),
'cache' => env('GLIDE_CACHE', storage_path('app/public/cache')),
Storage Backends: Use Flysystem adapters for cloud storage (S3, GCS):
$source = new Filesystem(new S3Adapter([
'bucket' => 'my-bucket',
'key' => 'images',
]));
$server = new Server($source, $cache, $api);
Path Resolution:
/images/user/photo.jpg may fail if base_url isn’t set.base_url in ServerFactory:
ServerFactory::create(['base_url' => '/images']);
Image Driver Performance:
blur=50). Use imagick or libvips for better performance.ServerFactory:
ServerFactory::create([
'image_manager' => new ImageManager(['driver' => 'imagick']),
]);
Cache Invalidation:
delete() for both source and cache.Query String Parsing:
&, =) in filenames may break parsing.rawurlencode():
$path = rawurlencode($path);
Log Manipulation Steps: Enable debug mode to log API calls:
$server->getApi()->setDebug(true);
Check Cache Directory:
Verify write permissions for the cache folder (e.g., storage/app/public/cache).
Validate Image Paths:
Use Filesystem::has() to check if source images exist before processing.
Custom Responses:
Extend League\Glide\Response\AbstractResponse to add headers or modify output:
class CustomResponse extends AbstractResponse {
public function getHeaders() {
return ['X-Custom-Header' => 'Glide'];
}
}
Register in ServerFactory:
ServerFactory::create(['response' => new CustomResponse()]);
Signature Validation: Secure URLs with HTTP signatures:
$url = $server->getImageUrl('photo.jpg', ['w' => 300], 3600, 'secret-key');
Validate in middleware:
if (!$server->validateSignature(request()->query())) {
abort(403);
}
Event Listeners:
Hook into Glide’s lifecycle (e.g., post-processing) via League\Glide\Events\ImageProcessed:
event(new ImageProcessed($image, $path, $query));
Service Provider:
Bind ServerFactory in AppServiceProvider:
$this->app->singleton(ServerFactory::class, function () {
return new ServerFactory([
'source' => storage_path('app/public/images'),
'cache' => storage_path('app/public/cache'),
]);
});
Blade Directives: Create a helper for Blade templates:
Blade::directive('glide', function ($path) {
return "<?php echo app('glide')->getImageUrl($path, request()->query()); ?>";
});
Usage:
<img src="{{ glide('photo.jpg?w=300') }}" alt="Photo">
Artisan Commands: Add a command to clear cache:
Artisan::command('glide:clear', function () {
$server = app(ServerFactory::class)->create();
$cache = $server->getCache();
$cache->delete($cache->listContents()['contents']);
});
How can I help you explore Laravel packages today?