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

  $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

  try {
    $imagefile = 'photo.jpg';
    $image = new Imagick();
    $type = exif_imagetype($imagefile);;
    header('Content-type: ' .image_type_to_mime_type($type));
    echo $image;
    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
PHP manual - image processing and generation
imagick changelog
PHP Imagick - tutorial with examples
Google PageSpeed documentation - performance best practices for websites - optimize caching