intervention/image
Intervention Image is a PHP image handling and manipulation library for Laravel and other frameworks. It provides a fluent API for resizing, cropping, encoding, watermarking, and optimizing images, with drivers for GD and Imagick and easy integration via service providers.
Installation:
composer require intervention/image
Ensure your server has either GD (php-gd) or Imagick (php-imagick) installed.
Basic Usage:
use Intervention\Image\Facades\Image;
$img = Image::make(public_path('image.jpg'));
$img->resize(300, 200)->save(public_path('resized.jpg'));
First Use Case:
$img = Image::make(storage_path('original.jpg'))
->fit(150, 150)
->save(storage_path('thumbnail.jpg'));
Intervention\Image\Facades\Image for quick usage.ImageManager::gd() or ImageManager::imagick() for explicit driver control.Image Processing Pipeline:
$img = Image::make($path)
->resize(null, 800, function ($constraint) {
$constraint->aspectRatio();
})
->watermark(public_path('watermark.png'), 'bottom-right', 10, 10)
->encode('jpg', 90)
->save($outputPath);
Dynamic Resizing with Conditions:
$img = Image::make($request->file('image'));
if ($request->input('is_thumbnail')) {
$img->fit(200, 200);
} else {
$img->resize(1200, null, function ($constraint) {
$constraint->upsize();
});
}
Batch Processing:
$files = Storage::disk('s3')->files('images/');
foreach ($files as $file) {
$img = Image::make(storage_path("tmp/{$file}"))
->resize(800, 600)
->save(storage_path("processed/{$file}"));
}
Laravel Filesystem:
Use Storage::disk() to read/write images from S3, local storage, etc.
$img = Image::make(Storage::disk('s3')->path('image.jpg'));
Jobs/Queues: Offload heavy processing to queues:
ProcessImageJob::dispatch($imagePath, $outputPath, $resizeTo);
Middleware: Validate/resize uploads before storage:
public function handle(Request $request, Closure $next) {
if ($request->hasFile('image')) {
$img = Image::make($request->file('image'));
$img->resize(1024, 1024)->save($request->file('image')->path());
}
return $next($request);
}
API Responses: Return processed images directly:
return response($img->encode(), 200, [
'Content-Type' => $img->mime(),
]);
Driver Incompatibility:
if (!ImageManager::gd()->check()) {
throw new \RuntimeException('GD library is not installed.');
}
Memory Limits:
memory_limit. Use libvips (if available) for memory efficiency:
$img = Image::make($path)->driver('vips')->resize(...);
Path Handling:
$img->text('Hello', public_path('fonts/arial.ttf'), 20)->draw();
Animated Images (GIF/WEBP):
$img = Image::make($path)->decode();
foreach ($img->frames() as $frame) {
$frame->save("frame_{$frame->index}.jpg");
}
Color Space Issues:
$img->colorize(function ($color) {
return $color->convert('rgb');
});
Inspect Image Data:
$img->debug()->save('debug.jpg'); // Shows bounding box, dimensions
var_dump($img->colors()); // Inspect palette
Error Handling: Wrap operations in try-catch:
try {
$img->resize(10000, 10000); // May fail for small images
} catch (\Intervention\Image\Exception\NotReadableException $e) {
Log::error($e->getMessage());
}
Driver-Specific Quirks:
imagick extension.Custom Drivers:
Extend Intervention\Image\Driver\DriverInterface for new backends (e.g., Cloudinary).
Modifier Chaining: Create reusable modifier chains:
$img->apply(function ($img) {
$img->resize(800, 600)->sharpen(10);
});
Color Manipulation: Use the new color system (v4+) for advanced effects:
$img->colorize(function ($color) {
return $color->adjust()->lightness(0.2);
});
Event Listeners:
Hook into image processing events (e.g., image.saving):
event(new ImageSaving($img, $path, $format));
Cache Drivers:
$img = Image::cache(function ($image) {
$image->resize(1000, 1000);
}, 'unique_key')->response();
Lazy Loading: Defer processing until response:
return Image::make($path)->resize(800, 600)->stream();
Format-Specific Optimizations:
$img->encode('jpg', 80, ['quality' => 80, 'progressive' => true]);
How can I help you explore Laravel packages today?