image(): echo $thumbnail->image('picture.jpg', array( 'alt' => 'test thumbnail', 'width' => '100', 'height' => '75' ) ); This will take the image 'picture.jpg' (/app/webroot/img/picture.jpg) and returns the following html code: test thumbnail It will make sure the thumbnail is created in /app/webroot/img/thumbs/100x75/ if it didn't exist already. Notes: - tested with cakephp 1.2 and phpthumb 1.7.9 - place this file (thumbnail.php) in /app/views/helpers/ - make sure phpthumb is located in one of the vendors directories. ie: vendors/phpthumb/phpthumb.class.php - create a directory named /app/webroot/img/thumbs that is writable by the webserver - include this helper on the controller level: var $helpers = array('Html', 'Thumbnail'); References: - original source of the helper: http://www.studiocanaria.com/articles/cakephp_thumbnail_helper - phpthumb: http://phpthumb.sourceforge.net/ */ class ThumbnailHelper extends AppHelper { /* * Default parameters for phpthumb * */ var $defaultParameters = array( 'w' => 320, // width 'h' => 200, // height 'f' => 'jpeg', // filetype 'q' => 80 // jpg quality ); var $helpers = array('Html'); /*** * Generates a thumbnail file and returns the path (uses caching) * * @param string $source_file Absolute path to the original image * @param array $parameters Array of phpthumb parameters, please consult "phpthumb.readme.txt" for more information * @param string $hash If supplied, the hash will be used as filename for the generated thumnail */ function render($source_file, $parameters = null, $hash = '') { // merge parameters if (is_array($parameters)) { $parameters = array_merge($this->defaultParameters, $parameters); } else { $parameters = $this->defaultParameters; } $path = pathinfo($source_file); // if hash isn't given, use original file name if ($hash == '') { $thumb_file = $path['basename']; } else { $thumb_file = $hash . '.' . $path['extension']; } // configure 'storage path' as "(webroot/img/)thumbs/{w}x{h}/{hash}.jpg" $thumb_url = 'thumbs' . DS . $parameters['w'] . 'x' . $parameters['h'] . DS . $thumb_file; $thumb_file = WWW_ROOT . 'img' . DS . $thumb_url; // if the file doesn't exist yet, create it if (!is_file($thumb_file)) { //import phpThumb class app::import( 'Vendor', 'phpthumb', array('file' => 'phpthumb' . DS . 'phpthumb.class.php') ); // create destination directory if it doesn't exist yet $destdir = dirname($thumb_file); if (!is_dir($destdir)) { // !!! initially you have to make sure the server can write // the 'storage path' !!! mkdir($destdir, 0777, true); } // instantiate and configure phpthumb object $thumb = new phpthumb; $thumb->setSourceFilename($source_file); // set the default parameters foreach($this->defaultParameters as $parameter => $value) { $thumb->setParameter($parameter, $value); } // now (over)write given parameters if (is_array($parameters)) { foreach($parameters as $parameter => $value) { $thumb->setParameter($parameter, $value); } } // check if we need to adjust the output format if (low($path['extension']) == 'png' or low($path['extension']) == 'gif') { $thumb->setParameter('f', $path['extension']); } // generate thumbnail if($thumb->GenerateThumbnail()) { $thumb->RenderToFile($thumb_file); } else { debug('phpthumb error: ' . $thumb->fatalerror); debug($thumb->debugmessages); } } return $thumb_url; } /** * Creates a formatted IMG element * * @param string $path Path to the image file, relative to the app/webroot/img/ directory. * @param array $htmlAttributes Array of HTML attributes. * @return string */ function image($path, $htmlAttributes = null) { $picture_path = WWW_ROOT . 'img' . DS . $path; $parameters = $this->defaultParameters; // make sure the parameters for phpthumb and html will match if (isset($htmlAttributes['width'])) { $parameters['w'] = $htmlAttributes['width']; } else { $htmlAtributes['width'] = $parameters['w']; } if (isset($htmlAttributes['height'])) { $parameters['h'] = $htmlAttributes['height']; } else { $htmlAttributes['height'] = $parameters['h']; } // get a link to the thumb version $thumb = $this->render($picture_path, $parameters); // generate html for displaying the image echo $this->Html->image($thumb, $htmlAttributes); } } ?>