Skip to main content

How to change the size of images in a template automatically (v2)

Tuskfish has an automatic image scaling and caching system that makes repurposing images a breeze. When you upload a nice master image with your content you often want to display it at smaller sizes in different contexts. Tuskfish can generate thumbnails to suit the needs of your templates automatically. Rather than accessing the image property of a content item directly, call the function cachedImage() in a template, passing in the desired width as an argument, for example:

<img src="<?php echo $content->cachedImage(500); ?>" alt="<?php echo $content->getCaption(); ?>"/>

Tuskfish will use the master image you uploaded with the content item to create a thumbnail 500 pixels wide and save it in the cache. The next time this template is called in a page load Tuskfish will check the cache to see if the thumbnail already exists, and since it does, your site will save CPU load and bandwidth.

If you later decided that 500 pixels was too big, and wanted to reduce the size of the images in a given template to 150 pixels wide, you would just change the argument to 150 in the template code. Tuskfish will then build and cache thumbnails at the new size for you automatically, as pages are requested. You don't need to do anything, it will "just happen". Cool, huh?

For best results please ensure your images have been converted to the sRGB colour space before you upload them, as PHP's GD library is not colour space aware. This will prevent unexpected shifts in colour.

Copyright, all rights reserved.