Generating on-the-fly images for Perch CMS

Since going Freelance I’ve been doing a fair bit of CMS integration for existing sites or for local graphic agencies that want something that offers simple content management quickly and cheaply.

Normally my CMS of choice is ExpressionEngine but in most cases this would just be overkill – the CMS is too complex and the license cost too great for smaller projects that only need a quick blog or image gallery on an otherwise static site.
Enter the fantastic Perch Content Management System – an extremely user-friendly and coder-friendly way of adding simple CMS functionality to otherwise static sites.

It differs from other CMS’ I’ve used in that is ‘template first’ – in other words the templates define the content management and not the other way round.
This is no bad thing but the one area where it falls short is with images.

Perch is rather cunning and you can set in advance a pre-defined thumbnail size for use in templates so that when an image is uploaded to the CMS it automagically crops images to the appropriate sizes for use in the front-end. However this becomes a problem if you or a client then decided to resize image thumbnail sizes in your templates.

This happened to me recently on a site that featured around fifty images entered via the Perch control panel.
The client wanted larger thumbnails and the only way to achieve this in Perch would be to redefine the thumbnail size in the CMS templates and upload all the images. Again.

This was less than ideal, so having done some image maniulation in PHP before I decided to write a wee add-on so that if you change the image crop sizes then new image thumbnails will be generated on-the-fly when they are requested.

The premise is simple. Perch appends a simple parameter to image filenames when it generates new image thumbnails.
A .htaccess in the resources folder directs all requests that aren’t for existing files to a PHP script.
The PHP script checks to see if a source image exists, calculates the resize dimensions in the requested filename and then redraws a new thumbnail with these dimensions.

This has been tested on one Perch build but obviously the more the merrier so would be really grateful if anyone using Perch could give it a try.
Installation and usage instructions are simple and contained in the acommpanying README file.
The files can be downloaded from the project Github repository

I welcome any feedback, suggestions or contributions to the code and please note that the PHP is pretty dirty.
I have no doubt that it could be tidied up substantially!

Comments

Please add your thoughts …

You can use Textile

Nice initiative! Never had the time (read: guts) to create a Perch plugin/add-on w/ that functionality.
Maybe this script will do the heavy lifting.