Advertisement
Guest User

thumbnail with cache

a guest
Jun 26th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.64 KB | None | 0 0
  1. function render( $image, $params ) {
  2.         //Set defaults
  3.         $path = '';
  4.         $width = 150;
  5.         $height = 225;
  6.         $quality = 75;
  7.         $extension = 'jpg';
  8.         //Extract Parameters
  9.         if( isset( $params[ 'path' ] ) ) {
  10.             $path = $params[ 'path' ] . DS;
  11.         }
  12.         if( isset( $params[ 'width' ] ) ) {
  13.             $width = $params[ 'width' ];
  14.         }
  15.         if( isset( $params[ 'height' ] ) ) {
  16.             $height = $params[ 'height' ];
  17.         }
  18.         if( isset( $params[ 'quality' ] ) ) {
  19.             $quality = $params[ 'quality' ];
  20.         }
  21.         if( isset( $params[ 'extension' ] ) ) {
  22.             $extension = $params[ 'extension' ];
  23.         }
  24.         //import phpThumb class with CakePHP
  25.         App::import( 'Vendor', 'phpthumb', array( 'file' => 'phpThumb' . DS . 'phpthumb.class.php' ) );
  26.         $thumbNail = new phpthumb;
  27.     //WWW_ROOT and DS are CakePHP global for web root folder and Directory Seperator
  28.         $thumbNail->src = WWW_ROOT . 'img' . DS . $path . $image;
  29.         $thumbNail->w = $width;
  30.         $thumbNail->h = $height;
  31.         $thumbNail->q = $quality;
  32.         $thumbNail->config_imagemagick_path = '/usr/bin/convert';
  33.         $thumbNail->config_prefer_imagemagick = false;
  34.         $thumbNail->config_output_format = $extension;
  35.         $thumbNail->config_error_die_on_error = true;
  36.         $thumbNail->config_document_root = '';
  37.     //APP is a CakePHP global for the APP folder
  38.         $thumbNail->config_temp_directory = APP . 'tmp';
  39.         $thumbNail->config_cache_directory = WWW_ROOT . 'img' . DS . 'thumbs' . DS;
  40.         $thumbNail->config_cache_disable_warning = true;
  41.         $cacheFilename = $image;
  42.         $thumbNail->cache_filename = $thumbNail->config_cache_directory . $cacheFilename;
  43.        
  44.         if( !is_file( $thumbNail->cache_filename ) ) {
  45.             if( $thumbNail->GenerateThumbnail() ) {
  46.                 $thumbNail->RenderToFile( $thumbNail->cache_filename );
  47.             }
  48.         }
  49.  
  50.         if( is_file( $thumbNail->cache_filename ) ) {
  51.     //File is a CakePHP Class that help manipulating files, I use it here to get the "last change date"
  52.             $fullSizedImage = new File( $thumbNail->src );
  53.             $thumbNailImage = new File( $thumbNail->cache_filename );
  54.             //update thumbnail if original one has been updated
  55.             if( $fullSizedImage->lastChange() > $thumbNailImage->lastChange() ) {
  56.                 if( $thumbNail->GenerateThumbnail() ) {
  57.                     $thumbNail->RenderToFile( $thumbNail->cache_filename );
  58.                 }
  59.             }
  60.             return $cacheFilename;
  61.         }
  62.        
  63.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement