>_ DevTrendsen

Language

Home

Languages

Sections

Frontend Backend Mobile DevOps AI / ML GameDev Blockchain Embedded Security
PHP

How to Stop Suffering from Working with Graphics in PHP

Latest Version Build Status Monthly Downloads Support me on Ko-fi

Anyone who has ever written thumbnail generation in pure PHP using the GD extension remembers this pain. A dozen low-level functions with cryptic signatures, manual coordinate calculations, and constant memory leaks. With Imagick, the syntax is slightly better, but it has its own zoo of methods.

The Intervention Image library has been solving exactly this problem for over ten years. It hides system extensions behind a unified and readable interface. The project recently reached version v3/v4, where the architecture was completely rewritten for modern PHP versions.

What's inside the library

The project offers a convenient wrapper around graphics engines. You write readable chainable code, and under the hood the library makes the necessary system calls on its own.

The main highlight here is swappable drivers. Right now the project supports:

  • GD (standard library available on nearly any hosting)
  • Imagick (extension based on ImageMagick)
  • libvips (fast engine with low memory consumption via a separate driver)

If the server doesn't have ImageMagick, you simply switch the driver to GD without rewriting the application's business logic. For high-load services, you can connect libvips, which works many times faster and handles RAM more carefully when processing heavy images.

Another nice detail is full animated image support (GIF, WebP) on all available drivers. Previously, working with animations required being tightly coupled to Imagick.

What the code looks like

To get started, install the library via Composer:

composer require intervention/image

You'll need PHP 8.3 or newer, the mbstring extension, and one of the graphics modules of your choice.

Here's a basic example that scales an image, applies a watermark, and saves the result as WebP or JPEG:

use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver as GdDriver;
use Intervention\Image\Alignment;
use Intervention\Image\Format;

// Инициализируем менеджер с нужным драйвером
$manager = ImageManager::usingDriver(GdDriver::class);

// Читаем исходный файл
$image = $manager->decodePath('images/example.webp');

// Пропорционально меняем размер по высоте
$image->scale(height: 300);

// Накладываем водяной знак в правый нижний угол
$image->insert('images/watermark.png', alignment: Alignment::BOTTOM_RIGHT);

// Кодируем результат с заданным качеством
$encoded = $image->encodeUsingFormat(Format::JPEG, quality: 65);

// Сохраняем готовый файл
$encoded->save('images/example.jpg');

The syntax relies on PHP 8 named arguments and strict typing with enums like Alignment::BOTTOM_RIGHT and Format::JPEG. Making mistakes with parameters is now much harder, and IDE autocompletion works out of the box.

Typical use cases

The package isn't tied to a specific framework, so it's equally easy to integrate into Laravel, Symfony, or a RoadRunner microservice.

  1. Avatar and thumbnail generation. Cropping, fitting into a square (crop, cover, scale), removing metadata before publishing.
  2. On-the-fly content optimization. Converting heavy PNG and JPEG to modern WebP or AVIF with compression.
  3. Copyright protection. Batch watermarking and logo overlays before serving files to CDN.
  4. Working with animations. Cropping or resizing GIFs without losing frames.

Is it worth adding to a project

If you're writing in PHP 8.3+ and need to process user files, Intervention Image will save a ton of time. Essentially, it's the de facto standard in the PHP ecosystem when it comes to graphics.

One nuance: if the project is stuck on old PHP versions like 7.4 or 8.1, you'll have to stay on the v2 branch, which has a completely different API. But for a fresh stack, the library fits perfectly.

Related projects