Creating dynamic thumbnails (or just resized images) with PHP is a pretty common task, but surprisingly not something readily available from the open-source community. There are a couple classes out there, but in my experience are either entirely non-functional or way more than is needed. Assuming you have ImageMagick (not the PHP extension Imagick – just plain ol’ ImageMagick), the following is a super-basic script that’ll create and output dynamic thumbnails. Even caching is squeezed into the following 20 lines. This script works, but is not intended to function as a plugin – feel free to modify at will.

use is simple, and all parameters are required:

The resize will attempt to fit the w/h dimensions as closely as possible without distorting the image.

The script first checks to see if the size for that image has already been created – if it is, it’s file data is returned. If not, a directory is created in $thumb_dir and named according to the size passed (e.g., w=100&h=75 would create images/thumbs/100×75/pic.jpg). By using -thumbnail (instead of -resize), we strip all metadata from the image and the filesize is optimal.