This thumbnail helper is based on the one published by Studio Canaria. Additional features are multiple size caching and an extra method which is a truly drop-in replacement for $html->image().
Usage:
<?php
echo $thumbnail->image('picture.jpg', array(
'alt' => 'test thumbnail',
'width' => '100',
'height' => '75'
)
);
// result:
// <img src="/img/thumbs/100x75/picture.jpg"
// alt="test thumbnail" height="75" width="100">
?>
Installation:
- Download thumbnail.txt
- Rename it to thumbnail.php and place it in /app/views/helpers/
- download phpThumb()
- Extract the phpThumb archive in a directory named /app/vendors/phpthumb/
- Add ‘Thumbnail’ to the ‘helpers’ array your controller
- Create a directory named ‘thumbs’ in /app/webroot/img/. Make sure it’s writable for the webserver.
That’s it!
In the above example, the thumb helper will first check if there is a file named /app/webroot/img/thumbs/100×75/picture.jpg. If it isn’t there yet, it will instantiate phpThumb and create the thumbnail.
For more advanced usage, there’s also the render() method. It will give you the flexibility to use images located outside /app/webroot/img and also make use of most of the phpThumb options.
Advanced example:
<?php $thumb = $thumbnail->render( $data['Picture']['path'], array( 'w' => 320, 'h' => 200, 'ra' => 10 ), '5271a9e66c99c84c092d7fd81cebcd57' ); echo $html->image($thumb); ?>
This will generate a picture named /app/webroot/img/thumbs/320×200/5271a9e66c99c84c092d7fd81cebcd57.jpg, which is a 10 degrees rotated thumbnail of the original image specified with $data['Picture']['path'].
Hi! The thumbnail.txt file is not downloadable (error 404), could you fix it, please?
The link to thubnail.txt has been fixed now!
Work at something you enjoy and that’s worthy of your time and talent.