Intervention Image
| Language | PHP 8.3 or later |
|---|---|
| License | MIT |
| Repository | github.com/Intervention/image |
| Documentation | image.intervention.io/v4 |
| Depends on | GD, Imagick or libvips (separate intervention/image-driver-vips package); the exif extension for metadata |
| Read / write | yes / yes; baseline and progressive, quality, optional metadata stripping; Exif reading |
Intervention Image wraps PHP’s image extensions in one object-oriented API. You choose a driver (GD, Imagick or libvips) once; the rest of the code is the same. By default it applies the Exif orientation when it decodes a JPEG, reads Exif data into a collection you can query with dot notation, and encodes to JPEG, PNG, WebP, AVIF, GIF and other formats.
Version 4 (current) renamed the main entry points from version 3: ImageManager::usingDriver(), decodePath() instead of read(), encodeUsingFormat() instead of toJpeg(). Examples below use version 4.
Installation#
composer require intervention/image
Basic usage: resize a photo and save as JPEG#
<?php
require 'vendor/autoload.php';
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\ImageManager;
$manager = ImageManager::usingDriver(Driver::class); // or Drivers\Imagick\Driver
$image = $manager->decodePath('photo.jpg'); // auto-oriented by default
echo $image->width(), ' x ', $image->height(), "\n";
$image->scaleDown(width: 1200); // keep aspect ratio, never upscale
$image->save('out.jpg', quality: 85); // format from the file extension
How to encode a progressive JPEG#
use Intervention\Image\Encoders\JpegEncoder;
$encoded = $image->encode(new JpegEncoder(quality: 80, progressive: true, strip: true));
$encoded->save('out.jpg');
// or send it: header('Content-Type: image/jpeg'); echo $encoded;
strip: true removes metadata (driver support varies; it applies with Imagick and libvips).
How to read Exif data#
$image = $manager->decodePath('photo.jpg');
echo $image->exif('IFD0.Model'), "\n";
echo $image->exif('EXIF.DateTimeOriginal'), "\n";
print_r($image->exif('GPS')); // section array
Exif is read with PHP’s exif_read_data, grouped by section (IFD0, EXIF, GPS, COMPUTED).
How to make square thumbnails#
$image = $manager->decodePath('photo.jpg');
$image->cover(300, 300); // resize and crop to exactly 300x300, centered
$image->save('thumb.jpg', quality: 80);
How to turn off auto-orientation#
Driver options are passed to usingDriver:
$manager = ImageManager::usingDriver(Driver::class, autoOrientation: false, strip: true);
Tips#
- Transparent areas become the configured
backgroundColor(white by default) when you encode to JPEG. - Use the Imagick or libvips driver for large photos; GD loads the whole image into PHP memory.
encodeUsingFormat(Format::JPEG, quality: 70)picks the encoder by format when you don’t save to a file.