cakePHP Thumbnail Helper with GIF, PNG and JPG support
Published: on 8/9/08 | Comments (2)
Hi all, a couple of weeks I got a request to add PNG and GIF support to my cakePHP thumbnail helper, so thought I'd post a quick updated version which includes this functionality.
Not a difficult one, I admit as the excellent phpThumb class library already includes this functionality, so just a matter of defining a default in the variable $extension, checking for an extension key in params, then setting it dynamically. In fact the only reason I didn't do this was because I didn't need it for the site I was working on at the time of writing the original article.
To use the updated version just overwrite your existing helper with the code below, make sure it's included within your helper array with:
var $helpers = array('Thumbnail');
Then within your view file you can do something like the following:
echo $html->image(
'thumbs/'.$thumbnail->render(
'picture01.jpg',
array(
'path'=>'pictures',
'width'=>100,
'height'=>150,
'quality'=>80,
'extension'=>'png'
)
)
);
The revised code is below:
<?php
class ThumbnailHelper extends AppHelper {
function render($image,$params){
//Set defaults
$path='';
$width=150;
$height=225;
$quality=75;
$extension='jpg';
//Extract Parameters
if(isset($params['path'])){
$path = $params['path'].DS;
}
if(isset($params['width'])){
$width = $params['width'];
}
if(isset($params['height'])){
$height = $params['height'];
}
if(isset($params['quality'])){
$quality = $params['quality'];
}
if(isset($params['extension'])){
$extension = $params['extension'];
}
//import phpThumb class
app::import('Vendor','phpthumb',array('file'=>'phpThumb'.DS.'phpthumb.class.php'));
$thumbNail = new phpthumb;
$thumbNail->src = WWW_ROOT.'img'.DS.$path.$image;
$thumbNail->w = $width;
$thumbNail->h = $height;
$thumbNail->q = $quality;
$thumbNail->config_imagemagick_path = '/usr/bin/convert';
$thumbNail->config_prefer_imagemagick = true;
$thumbNail->config_output_format = $extension;
$thumbNail->config_error_die_on_error = true;
$thumbNail->config_document_root = '';
$thumbNail->config_temp_directory = APP . 'tmp';
$thumbNail->config_cache_directory = WWW_ROOT.'img'.DS.'thumbs'.DS;
$thumbNail->config_cache_disable_warning = true;
$cacheFilename = $image;
$thumbNail->cache_filename = $thumbNail->config_cache_directory.$cacheFilename;
if(!is_file($thumbNail->cache_filename)){
if($thumbNail->GenerateThumbnail()) {
$thumbNail->RenderToFile($thumbNail->cache_filename);
}
}
if(is_file($thumbNail->cache_filename)){
return $cacheFilename;
}
}
}
?>
OK, thats it for today, hope that helps a few of you, in particular DAA who made the request, till the next time
happy baking!
Comments
-
1:
keymaster says
on 6/11/08
Looks like there may be a typo in your code.
line 23: if(isset($params['extention'])){
should be: if(isset($params['extension'])){
... change 't' to 's' in 'extension'.
-
2:
Peter Butler says
on 7/11/08
@ keymaster - well spotted and thanks for pointing it out, I have now corrected this.
