It may seem counter-intuitive to use anything other than or to resize images: why on earth would you rescale an image with a server-side technique when the same could be done easily and quickly with a modification to the image’s width and height in your code?

The need for a server-side image resize will often occur when you are interacting with a service external to your own site: an API that requests an image in a particular size and format, for example. Another possibility is : if you allow users to upload images to your site, you’ll want to be sure they are scaled and cropped to specific dimensions. Under such circumstances, CSS is useless: the image must be the right size and format before it is used. Another good example is social media sites’ request for a thumbnail image to associate with a web page or blog post, during a “share” or “like” action.

Photograph of a Spanish lighthouse at dusk

To gain access to image metadata, you must load the file a server-side language. You can dynamically load the image in any way you please, including the methods I’ve shown in the past: the only condition is that PHP must be able to address the image file. Here, to make things clearer, I’ll use the image’s filename directly, rather than using a variable.

From PHP’s perspective, IPTC metatags are secreted in the getimagesize function. To make this information visible, we’ll create an array, fill it with getimagesize data, and then use print_r to make the keys of the array visible (I’ll wrap the print_r in a <pre> to format the array in a more presentable fashion:

A more elegant solution to captioning images for galleries and portfolios, and one that requires considerably less work than adding a URL variable, is to create a dependency on an accurate, descriptive image filename. In essence, this is a variation on the self-made pages technique I have demonstrated before.

Of course, your gallery images should already have descriptive filenames, with hyphens in place of spaces. Assuming that you have used a GET variable that contains the image filename (I’m going to call the variable $img for the purposes of this example), it should not be a stretch to add something like the following: