Working with Images in PHP: GD and Imagick Libraries
Image processing is one of the key tasks in web development. In PHP, two main libraries are used for this: GD (built-in) and Imagick (a wrapper over ImageMagick). In this article, we will explore their capabilities, learn how to resize, crop, add watermarks, and optimize images.
1. GD Library
GD is a standard PHP extension available on most hosting platforms. It allows you to create, modify, and save images in JPEG, PNG, GIF, and WebP formats.
Enabling and checking:
<?php// Check if the GD extension is installedif (extension_loaded('gd') && function_exists('gd_info')) { echo "GD is installed: "; print_r(gd_info());} else { echo "GD not found";}?>1.1 Resizing an Image
The most common scenario is reducing an image to the desired dimensions while maintaining proportions.
<?phpfunction resizeImage($sourcePath, $destPath, $newWidth, $newHeight) { // Get dimensions of the source image list($width, $height, $type) = getimagesize($sourcePath); // Create a resource from the source file switch ($type) { case IMAGETYPE_JPEG: $source = imagecreatefromjpeg($sourcePath); break; case IMAGETYPE_PNG: $source = imagecreatefrompng($sourcePath); break; case IMAGETYPE_GIF: $source = imagecreatefromgif($sourcePath); break; default: return false; } // Create a new image $newImage = imagecreatetruecolor($newWidth, $newHeight); // Preserve transparency for PNG/GIF imagealphablending($newImage, false); imagesavealpha($newImage, true); // Copy and resample imagecopyresampled($newImage, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); // Save the result imagejpeg($newImage, $destPath, 85); // Quality 85% // Free memory imagedestroy($source); imagedestroy($newImage); return true;}
// Usage exampleresizeImage('photo.jpg', 'photo_thumb.jpg', 300, 200);?>1.2 Cropping an Image
Cropping allows you to extract the central part of an image at the desired size.
<?phpfunction cropCenter($sourcePath, $destPath, $cropWidth, $cropHeight) { list($width, $height) = getimagesize($sourcePath); $source = imagecreatefromjpeg($sourcePath); // Calculate center coordinates $x = ($width - $cropWidth) / 2; $y = ($height - $cropHeight) / 2; $newImage = imagecreatetruecolor($cropWidth, $cropHeight); imagecopyresampled($newImage, $source, 0, 0, $x, $y, $cropWidth, $cropHeight, $cropWidth, $cropHeight); imagejpeg($newImage, $destPath, 90); imagedestroy($source); imagedestroy($newImage);}
cropCenter('photo.jpg', 'photo_cropped.jpg', 400, 400);?>1.3 Adding a Watermark
Adding text or a logo over an image.
<?phpfunction addWatermarkText($sourcePath, $destPath, $text = "© PythonLib.ru") { $source = imagecreatefromjpeg($sourcePath); $width = imagesx($source); $height = imagesy($source); // Text color (white with transparency) $color = imagecolorallocatealpha($source, 255, 255, 255, 50); // Font size (using built-in font) $fontSize = 5; $x = 10; $y = $height - 20; imagestring($source, $fontSize, $x, $y, $text, $color); imagejpeg($source, $destPath, 90); imagedestroy($source);}
addWatermarkText('photo.jpg', 'photo_watermarked.jpg');?>