Published: on 8/9/08 | Comments (3)
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:
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
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'.
Studio Canaria is the web site of freelance web developer, Peter Butler. Articles on this site relate to designing, developing and marketing modern web applications.
CakePHP Auth Component - Users, Groups & Permissions Revisited
CakePHP Auth Component - Users, Groups & Permissions