
In web development you often need to work on images: avatars, banners, thumbnails or any other picture your site has to display. PHP ships with several libraries for handling images, chief among them GD and Imagick. This article walks through the main features of both, with code examples you can drop into your own PHP application.
Using the GD library in PHP
GD is a PHP library for manipulating images. It comes with most default PHP installations, which makes it easy to reach for. GD supports the following image formats: JPG, PNG, GIF, BMP and WBMP.
Loading and displaying an image with GD
To load an image with GD, use the imagecreatefrom*() function matching the image type (jpg, png, gif and so on). It returns a GD resource representing the image held in memory. You can then use header together with the image*() function for that format (imagejpeg, imagepng, imagegif and the rest) to send the image to the browser.
<?php
// Load the image into a GD resource
$image = imagecreatefromjpeg('image.jpg');
// Output the image
header('Content-Type: image/jpeg');
imagejpeg($image);Resizing an image with GD
To resize an image with GD, use the imagecopyresampled function. It copies and resizes a GD image to the dimensions you give it, interpolating along the way to keep the quality of the resized image up.
<?php
// Load the image into a GD resource
$image = imagecreatefromjpeg('image.jpg');
// Resize the image
$image_resized = imagecreatetruecolor(200, 200);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, 200, 200, imagesx($image), imagesy($image));
// Output the resized image
header('Content-Type: image/jpeg');
imagejpeg($image_resized);Adding text to an image with GD
To add text to an image with GD, use the imagettftext function. It draws text onto a GD image, taking the font, the size, the angle and the colour of the text.
<?php
// Load the image into a GD resource
$image = imagecreatefromjpeg('image.jpg');
// Add text to the image
$text_color = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, 20, 0, 10, 30, $text_color, 'arial.ttf', 'Hello World !');
// Output the image
header('Content-Type: image/jpeg');
imagejpeg($image);Using the Imagick library
Imagick is another PHP library for handling images. It goes further than GD, with layers and masks, format conversion, edge detection and noise reduction. Imagick supports a great many image formats, vector ones such as SVG included.
Before using Imagick, check that the imagick PHP extension is installed and enabled on your server. You can do that with the following command:
php -m | grep imagickIf Imagick shows up in the list of loaded extensions, you can use the library’s classes and methods in your PHP code. If it does not, you will have to install and enable the Imagick extension on your server first.
Loading and displaying an image with Imagick
To load an image with Imagick, create an Imagick object from the path to the image. That object represents the image held in memory, and its methods are how you work on it. To display the image, simply echo the Imagick object.
<?php
// Load the image into an Imagick object
$imagick = new Imagick('image.jpg');
// Output the image
header('Content-Type: image/jpeg');
echo $imagick;Resizing an image with Imagick
To resize an image with Imagick, use the resizeImage method on the Imagick object. It resizes the image to the dimensions you give it, using a resampling filter to keep the quality of the result up. Imagick offers several resampling filters, such as FILTER_LANCZOS, FILTER_BOX or FILTER_CUBIC.
<?php
// Load the image into an Imagick object
$imagick = new Imagick('image.jpg');
// Resize the image
$imagick->resizeImage(200, 200, Imagick::FILTER_LANCZOS, 1);
// Output the resized image
header('Content-Type: image/jpeg');
echo $imagick;Adding text to an image with Imagick
To add text to an image with Imagick, use the annotateImage method on the Imagick object. It draws text onto the image, taking the font, the size, the angle and the colour of the text.
<?php
// Load the image into an Imagick object
$imagick = new Imagick('image.jpg');
// Add text to the image
$draw = new ImagickDraw();
$draw->setFont('arial.ttf');
$draw->setFontSize(20);
$draw->setFillColor('black');
$imagick->annotateImage($draw, 10, 30, 0, 'Hello World !');
// Output the image
header('Content-Type: image/jpeg');
echo $imagick;
Conclusion
This article has covered the main features of the GD and Imagick libraries for handling images in PHP: loading and displaying an image, resizing it, and adding text to it. You now have working code to build these features into your own PHP application. Both libraries have plenty more methods and options worth exploring to go further with image processing in PHP.


