Intervention Image
| Language | PHP 8.3 or later |
|---|---|
| License | MIT |
| Repository | github.com/Intervention/image |
| Documentation | image.intervention.io |
| Depends on | ext-gd or ext-imagick (or libvips through a separate driver package) |
| Read / write | yes / yes, through the driver; interlaced and indexed (palette) PNG output |
Intervention Image is a Composer package that wraps GD or Imagick behind one object-oriented API: decode an image, chain operations like scale, cover, crop and insert, then encode to a format. You can switch drivers without changing your code. It’s popular in Laravel projects but has no framework dependency.
PNG support comes from the driver, so the limits of GD (no text chunks, 8 bits per channel) apply when you use the GD driver. This page shows version 4; version 3 used read() and toPng() instead of decodePath() and encodeUsingFormat().
Installation#
Shell
composer require intervention/image
Basic usage: read, resize and save a PNG#
PHP
<?php
require 'vendor/autoload.php';
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
$manager = ImageManager::usingDriver(Driver::class);
$image = $manager->decodePath('in.png');
echo $image->width() . ' x ' . $image->height() . "\n";
$image->scale(width: 400); // keeps the aspect ratio
$image->save('out.png'); // format from the extension
For Imagick, use Intervention\Image\Drivers\Imagick\Driver instead.
How to make a square PNG thumbnail#
cover resizes and crops to fill exactly the given size:
PHP
<?php
$image = $manager->decodePath('in.png');
$image->cover(200, 200);
$image->save('thumb.png');
How to save an indexed (palette) PNG#
PHP
<?php
use Intervention\Image\Encoders\PngEncoder;
$image = $manager->decodePath('in.png');
$image->reduceColors(256);
$encoded = $image->encode(new PngEncoder(indexed: true));
$encoded->save('palette.png');
How to convert PNG to JPEG or WebP#
PHP
<?php
use Intervention\Image\Format;
$image = $manager->decodePath('in.png');
$image->encodeUsingFormat(Format::WEBP, quality: 80)->save('out.webp');
$image->fillTransparentAreas('ffffff'); // JPEG has no alpha
$image->encodeUsingFormat(Format::JPEG, quality: 85)->save('out.jpg');
How to return a PNG in an HTTP response#
PHP
<?php
use Intervention\Image\Format;
$encoded = $manager->decodePath('in.png')
->scale(width: 800)
->encodeUsingFormat(Format::PNG);
header('Content-Type: ' . $encoded->mediaType());
echo $encoded->toString();
Tips#
$manager->decode($input)accepts a path, binary string, base64, data URI orSplFileInfo(e.g. an upload).- To get the size of an upload without decoding pixels, use PHP’s
getimagesize(). scaleDownandcoverDownnever enlarge small images.