PHP and images
The topics in this page: embedded EXIF thumbnails, resizing images, caching headers, reference links.
Embedded EXIF thumbnails
A simple way to dynamically generate multiple thumbnails for a photo gallery
is to use the embedded thumbnails in the EXIF data
that JPEG or TIFF image files taken by most digital cameras have.
Extracting the embedded thumbnail is more memory efficient than generating the thumbnail by resizing the original image.
PHP has an EXIF extension that can be used for reading the EXIF data. The easiest way to see if the EXIF extension is installed
and enabled is by running phpinfo().
There is an Exif Viewer add-on for Firefox that displays the Exif and IPTC data in local and remote JPEG images.
An example of PHP code to dynamically generate a thumbnail from the embedded EXIF thumbnail is
<?php
$imagefile = 'photo.jpg';
$thumb = exif_thumbnail($imagefile, $width, $height, $type);
if ($thumb!==false) {
header('Content-type: '.image_type_to_mime_type($type));
echo $thumb;
} else { echo "No thumbnail available"; }
}
?>Resizing images
One way to resize image files in PHP is by using Imagick.
Imagick is a native PHP extension to create and modify images using the ImageMagick API.
There are several Imagickfunctions for resizing images, for example Imagick::scaleImage,
Imagick::adaptiveResizeImage or
Imagick::resizeImage.
The adaptiveResizeImage functions resizes image adaptively with data-dependent triangulation. It avoids blurring across sharp color changes. It is most useful when used to shrink images slightly to a slightly smaller web site, it may not look good when a full-sized image is adaptively resized to a thumbnail.
The resizeImage function has the option of setting a filter, the Lanczos filter usually gives good results, but it is slower
Rezising images on the fly is putting more load on the server than downloading static images, but it is flexible and it is easier for managing photo galleries because an original image does not require additional static image files of different sizes.
An example for dynamically generating a resized image smaller than the original image using proportional scaling is
<?php
try {
$imagefile = 'photo.jpg';
$image = new Imagick();
$image->readImage($imagefile);
$type = exif_imagetype($imagefile);;
$image->resizeImage(0,$height,Imagick::FILTER_LANCZOS,1);
header('Content-type: ' .image_type_to_mime_type($type));
echo $image;
$image->clear();
$image->destroy();
}
catch(Exception $err)
{
echo $err->getMessage();
}
?>Caching headers
Dynamically generated images are not cached by browsers unless the HTTP server header indicates to browsers that they can do that. It is important to specify one of Expires or Cache-Control max-age, and one of Last-Modified or ETag, for all cacheable resources. When the images are dynamically generated using PHP, those additional HTTP server headers can be specified using the header function at the start of the file source code.
External reference links
- uk.php.net/manual/en/refs.utilspec.image.php
- PHP manual - image processing and generation
- pecl.php.net/package-changelog.php?package=imagick
- imagick changelog
- www.phpro.org/tutorials/Imagick.html
- PHP Imagick - tutorial with examples
- code.google.com/speed/page-speed/docs/caching.html
- Google PageSpeed documentation - performance best practices for websites - optimize caching