cakePHP Thumbnail Helper with GIF, PNG and JPG support

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

happy baking!


Comments

1: keymaster said

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 said

@ keymaster - well spotted and thanks for pointing it out, I have now corrected this.


3: JakeHilfter said I really liked this post. Can I copy it to my site? Thank you in advance.

Have Your Say

Comments are now closed for this article


About Studio Canaria

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.

Recent Comments

Dave on CakePHP Auth Component - Users, Groups & Permissions Revisited
I dont understand how you define permissions for each contoller to User. Do I have to manually...
SI on CakePHP Auth Component - Users, Groups & Permissions Revisited
Hi Peter,

I am having little issue logging into the application. I have 2 user groups both having...
Alig on CakePHP Auth Component - Users, Groups & Permissions Revisited
Thank you very much for this tutorial. After fighting for hours to get the cakephp ACL working,...
Benny on CakePHP Auth Component - Users, Groups & Permissions Revisited
thank you for your solution it's perfect , you are genius , thank you this is the best ( beside...
Gordon on CakePHP Auth Component - Users, Groups & Permissions Revisited
I'm having a similar problem to luis. I followed everything as instructed here up to the point...