Working with Images in PHP: resize, crop

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: Resizing and Cropping (resize, crop)



Image processing is one of the most common tasks in web application development. Uploading avatars, creating thumbnails, adapting images for different screen resolutions — all of this requires resizing and cropping. In PHP, there are two main tools for these purposes: the built-in GD extension and the more powerful Imagick library. In this article, we will cover both approaches with practical examples.



1. GD Extension (Standard Tool)



GD is the most popular and accessible extension for working with images. It is built into most PHP distributions and does not require additional installation. GD supports the main formats: JPEG, PNG, GIF, WebP.



1.1. Resizing with Aspect Ratio Preservation



A classic task is to reduce a large image to a specified width while maintaining the aspect ratio. Below is an example of a function that scales an image to a specified width.



<?phpfunction resizeImage($srcFile, $destFile, $newWidth) {    // Get information about the source image    list($width, $height, $type) = getimagesize($srcFile);

// Calculate the new height proportionally $ratio = $width / $height; $newHeight = $newWidth / $ratio;

// Create a canvas for the new image $srcImage = imagecreatefromjpeg($srcFile); // for JPEG $destImage = imagecreatetruecolor($newWidth, $newHeight);

// Copy and resample imagecopyresampled($destImage, $srcImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

// Save the result imagejpeg($destImage, $destFile, 90); // quality 90%

// Free memory imagedestroy($srcImage); imagedestroy($destImage);}

// Usage exampleresizeImage('photo.jpg', 'photo_thumb.jpg', 300);?>


For working with PNG or GIF, you need to use the imagecreatefrompng() and imagecreatefromgif() functions, and save via imagepng() or imagegif().



1.2. Center Cropping an Image



Often you need not just to reduce, but to cut out a square area from the center of the image. This is called centered crop.



<?phpfunction cropCenter($srcFile, $destFile, $cropSize) {    list($width, $height, $type) = getimagesize($srcFile);

// Determine coordinates for cutting a square from the center $srcX = ($width - $cropSize) / 2; $srcY = ($height - $cropSize) / 2;

// If the image is smaller than the required size, just scale it if ($width < $cropSize || $height < $cropSize) { $ratio = min($cropSize / $width, $cropSize / $height); $newWidth = $width * $ratio; $newHeight = $height * $ratio; $srcImage = imagecreatefromjpeg($srcFile); $destImage = imagecreatetruecolor($cropSize, $cropSize); // Fill with white background $white = imagecolorallocate($destImage, 255, 255, 255); imagefill($destImage, 0, 0, $white); // Copy with centering imagecopyresampled($destImage, $srcImage, ($cropSize - $newWidth) / 2, ($cropSize - $newHeight) / 2, 0, 0, $newWidth, $newHeight, $width, $height); } else { $srcImage = imagecreatefromjpeg($srcFile); $destImage = imagecreatetruecolor($cropSize, $cropSize); imagecopyresampled($destImage, $srcImage, 0, 0, $srcX, $srcY, $cropSize, $cropSize, $cropSize, $cropSize); }

imagejpeg($destImage, $destFile, 90); imagedestroy($srcImage); imagedestroy($destImage);}

// Example: 200x200 square from the centercropCenter('photo.jpg', 'photo_cropped.jpg', 200);?>


2. Imagick Library (Advanced Tool)



Imagick is a PHP wrapper for the powerful ImageMagick utility. It pre

Recommendations