league/glide
Glide is an on-demand PHP image manipulation library served over HTTP. Resize, crop, and apply effects with simple URL parameters, with automatic caching and far-future headers. Works with GD, Imagick, or libvips and Flysystem storage.
## Getting Started
### Minimal Setup in Laravel
1. **Installation**:
```bash
composer require league/glide
Basic Route Configuration (in routes/web.php):
use League\Glide\ServerFactory;
Route::get('/img/{path}', function ($path) {
$server = ServerFactory::create([
'source' => storage_path('app/public'),
'cache' => storage_path('app/cache/glide'),
'base_url' => '/img',
]);
return $server->getImageResponse($path, $_GET);
});
First Use Case:
storage/app/public (e.g., profile.jpg).<img src="/img/profile.jpg?w=200&h=200&fit=crop" alt="Profile">
w, h, fit) dynamically resize the image.storage/app/public.storage/app/cache/glide for performance.// In a controller or service
$server = app(ServerFactory::class)->create([
'source' => storage_path('app/public'),
'cache' => storage_path('app/cache/glide'),
]);
return $server->getImageResponse('products/123.jpg', ['w' => 800, 'h' => 600, 'fit' => 'crop']);
Laravel Middleware: Create middleware to handle Glide requests globally:
// app/Http/Middleware/GlideMiddleware.php
public function handle($request, Closure $next) {
if ($request->is('img/*')) {
$server = ServerFactory::create(config('glide'));
return $server->getImageResponse($request->path(), $request->query());
}
return $next($request);
}
Register in app/Http/Kernel.php:
protected $middleware = [
\App\Http\Middleware\GlideMiddleware::class,
];
Configuration:
Add to config/glide.php:
return [
'source' => storage_path('app/public'),
'cache' => storage_path('app/cache/glide'),
'base_url' => '/img',
'driver' => 'gd', // or 'imagick', 'vips'
];
// app/Services/GlideService.php
class GlideService {
protected $server;
public function __construct() {
$this->server = ServerFactory::create(config('glide'));
}
public function getImageUrl($path, array $params = []) {
return route('glide.image', ['path' => $path, 'query' => http_build_query($params)]);
}
public function getImageResponse($path, array $params = []) {
return $this->server->getImageResponse($path, $params);
}
}
{{ $glideService->getImageUrl('products/123.jpg', ['w' => 400, 'h' => 400]) }}
$server = ServerFactory::create([
'source' => storage_path('app/public'),
'cache' => storage_path('app/cache/glide'),
'signature_key' => config('app.key'),
'signature_secret' => config('glide.signature_secret'),
]);
$signedUrl = $server->getSignedUrl('products/123.jpg', ['w' => 300], '+1 hour');
Cache Directory Permissions:
Ensure storage/app/cache/glide is writable by the web server user (e.g., chmod -R 755 storage/app/cache/glide).
Symptom: Blank images or 500 errors when processing.
Base URL Mismatch:
If base_url is misconfigured, Glide may fail to locate source files.
Fix: Verify the base_url matches your route (e.g., /img in routes but base_url set to /images).
Driver Limitations:
blur=50).pecl install imagick).php-vips extension.Query String Order:
Glide processes parameters in a specific order (e.g., rect before fit). Incorrect order may yield unexpected results.
Example: ?rect=100,100,200,200&w=100&h=100 (correct) vs. ?w=100&h=100&rect=100,100,200,200 (may fail).
storage/app/cache/glide. If not, check file permissions or driver errors.$server->setDebug(true);
Errors will be logged to storage/logs/laravel.log.?w=100&h=100) before adding complex effects (e.g., blur, pixel).$server->outputImage('products/123.jpg', ['w' => 100, 'h' => 100, 'fit' => 'crop']);
blur values) on large images. Use libvips for better performance.Custom Manipulators: Extend Glide by adding custom manipulators (e.g., watermarks):
use League\Glide\Api\Manipulator\ManipulatorInterface;
class WatermarkManipulator implements ManipulatorInterface {
public function manipulate($image, $params) {
if (isset($params['watermark'])) {
$watermark = Intervention\Image::make(public_path('watermark.png'));
$image->insert($watermark, 'bottom-right', 10, 10);
}
return $image;
}
}
Register in ServerFactory:
$manipulators = [
new WatermarkManipulator(),
// ... other manipulators
];
$api = new League\Glide\Api\Api($imageManager, $manipulators);
Flysystem Adapters: Use custom Flysystem adapters (e.g., S3, Dropbox) for cloud storage:
$source = new League\Flysystem\Filesystem(
new League\Flysystem\AwsS3\AwsS3Adapter('bucket', 'key', 'secret')
);
$path and query parameters to prevent directory traversal:
$path = preg_replace('/[^a-z0-9_\-\/]/i', '', $path);
cache to a temporary directory (e.g., sys_get_temp_dir()) to avoid stale cached images during development.// app/Console/Commands/ClearGlideCache.php
public function handle() {
$cachePath = storage_path('app/cache/glide');
if (File::exists($cachePath)) {
File::delete
How can I help you explore Laravel packages today?