Working with Images in PHP: Overview of Libraries and Tools

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Images in PHP: From Basics to Professional Tools



Image processing is one of the key tasks in modern web application development. Whether it's uploading an avatar, creating thumbnails for a gallery, or applying watermarks — PHP provides several powerful tools for working with graphics. In this article, we will thoroughly examine three main approaches: the built-in GD library, the Imagick extension (based on ImageMagick), and the convenient Intervention Image wrapper.



1. GD Library — The Basic Tool in PHP



GD (Graphics Draw) is a built-in PHP library available "out of the box" on most hosting platforms. It allows you to create, resize, and apply effects to images in JPEG, PNG, GIF, and WebP formats.



Main features of GD:

  • Resizing and cropping
  • Applying text and watermarks
  • Applying color filters (grayscale, negative)
  • Creating images from scratch (charts, diagrams)


Example: resizing an image while maintaining proportions

<?phpfunction resizeImage($srcFile, $dstFile, $maxWidth = 300, $maxHeight = 300) {    list($origWidth, $origHeight, $type) = getimagesize($srcFile);        // Calculate new dimensions    $ratio = min($maxWidth / $origWidth, $maxHeight / $origHeight);    $newWidth = (int)($origWidth * $ratio);    $newHeight = (int)($origHeight * $ratio);        // Create images    $srcImg = imagecreatefromstring(file_get_contents($srcFile));    $dstImg = imagecreatetruecolor($newWidth, $newHeight);        // Copy with resampling    imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);        // Save the result    imagejpeg($dstImg, $dstFile, 85);    imagedestroy($srcImg);    imagedestroy($dstImg);}

// UsageresizeImage('upload/photo.jpg', 'upload/thumb.jpg', 200, 200);?>


Pros of GD: does not require installing additional extensions, lightweight, fast for simple operations.
Cons: limited format support (no TIFF, PSD), poor handling of transparency in PNG-8.



2. Imagick — Professional Image Processing



Imagick is a PHP extension that provides an interface to the powerful ImageMagick library. It supports over 200 file formats and allows performing complex operations: animation, color correction, working with ICC profiles.



Installation: sudo apt install php-imagick (Linux) or enabling the extension in php.ini.



Example: adding a watermark

<?phpfunction addWatermark($imagePath, $watermarkPath, $outputPath) {    $image = new Imagick($imagePath);    $watermark = new Imagick($watermarkPath);        // Positioning the watermark (bottom right corner)    $x = $image->getImageWidth() - $watermark->getImageWidth() - 20;    $y = $image->getImageHeight() - $watermark->getImageHeight() - 20;        // Overlay with transparency    $image->compositeImage($watermark, Imagick::COMPOSITE_OVER, $x, $y);        // Save    $image->writeImage($outputPath);        // Free memory    $image->clear();    $watermark->clear();}

addWatermark('photo.jpg', 'logo.png', 'photo_watermarked.jpg');?>


Pros of Imagick: rich functionality, high performance on complex operations, support for vector graphics (SVG).
Cons: requires installation on the server, consumes more memory than GD.



3. Intervention Image — A Convenient Wrapper for Laravel and Beyond



Intervention Image is a popular PHP library that provides a unified API for working

Recommendations