Working with Images in PHP: Resize, Crop, and Optimization
Image processing is one of the key tasks in web application development. Whether it's user avatars, news thumbnails, or product galleries — you almost always need to resize and crop images. In PHP, there are two main libraries for this: GD (built-in) and Imagick (an extension based on ImageMagick).
In this article, we will cover both approaches, show working code examples, and provide recommendations on which library to choose for your project.
1. GD Library (GDLib)
GD is a standard PHP extension available on almost any hosting. It supports basic formats: JPEG, PNG, GIF, WEBP (since PHP 7.1+). The downside is lower resampling quality compared to Imagick, but its capabilities are sufficient for most tasks.
1.1. Resizing with GD
The imagecopyresampled() function creates a new image with interpolation (smoothing). Example of scaling to a width of 300px while maintaining proportions:
<?phpfunction resizeImageGD($srcFile, $destFile, $newWidth = 300) { // Get the dimensions of the source image list($width, $height, $type) = getimagesize($srcFile); // Create a source image resource based on the type switch ($type) { case IMAGETYPE_JPEG: $src = imagecreatefromjpeg($srcFile); break; case IMAGETYPE_PNG: $src = imagecreatefrompng($srcFile); break; case IMAGETYPE_GIF: $src = imagecreatefromgif($srcFile); break; default: return false; } // Calculate the new height proportionally $ratio = $width / $height; $newHeight = intval($newWidth / $ratio); // Create an empty image of the required size $dst = imagecreatetruecolor($newWidth, $newHeight); // Enable alpha channel for PNG/GIF imagealphablending($dst, false); imagesavealpha($dst, true); // Resampling (copying with pixel recalculation) imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); // Save the result imagejpeg($dst, $destFile, 85); // quality 85% // Free memory imagedestroy($src); imagedestroy($dst); return true;}
// Usage:resizeImageGD('photo.jpg', 'photo_thumb.jpg', 300);?>1.2. Cropping with GD
For cropping, use the imagecopy() or imagecopyresampled() function specifying the area. Example — crop a central 200x200 square:
<?phpfunction cropCenterGD($srcFile, $destFile, $cropSize = 200) { list($width, $height, $type) = getimagesize($srcFile); // Determine the type $src = imagecreatefromjpeg($srcFile); // JPEG example // Calculate coordinates of the central square $x = ($width - $cropSize) / 2; $y = ($height - $cropSize) / 2; // If the source image is smaller than the crop size — do not crop if ($width < $cropSize || $height < $cropSize) { $cropSize = min($width, $height); $x = 0; $y = 0; } $dst = imagecreatetruecolor($cropSize, $cropSize); imagecopy($dst, $src, 0, 0, $x, $y, $cropSize, $cropSize); imagejpeg($dst, $destFile, 90); imagedestroy($src); imagedestroy($dst);}
cropCenterGD('photo.jpg', 'cropped.jpg', 200);?>2. Imagick Library
Imagick is a powerful extension that provides an object-oriented interface to ImageMagick. It supports over 200 formats, excellent resampling quality, and work with colors, filters, and animation. It requires installation on the server (not always available on cheap hosting