Ejemplo n.º 1
0
 /**
  * Resize an image
  *
  * Looks like a private function to me, but could be used as it is
  * 
  * @param $filename String Nom du fichier
  * @param $options Array Can override $settings and add these options :
  *				output : relative to 'folder' output image filename (default : $filename (overwrite))
  *				delete_source : if output is different from input, this set to true will delete the source file (default : false)
  * @return Returns true in case of success, false otherwise ($this->errors[$Model->name] gets populated)
  */
 function resizeImage(&$Model, $filepath, $options = array())
 {
     if (!file_exists($filepath)) {
         $this->errors[$Model->name][] = __('PhpThumb behavior : le fichier ', true) . $filepath . __(' n\'existe pas.', true);
         return false;
     }
     $settings = am(am($this->settings[$Model->name]['default'], array('output' => $filepath, 'delete_source' => false)), $options);
     App::import('Vendor', 'phpThumb', array('file' => 'phpThumb' . DS . 'phpthumb.class.php'));
     $phpThumb = new phpThumb();
     $phpThumb->setSourceFilename($filepath);
     $phpThumb->setParameter('w', $settings['width']);
     $phpThumb->setParameter('h', $settings['height']);
     if ($settings['zoomcrop'] === true) {
         //$phpThumb->setParameter('zc', 1);
     }
     if (!(0 == $settings['sx'] && 0 == $settings['sy'] && 0 == $settings['sw'] && 0 == $settings['sh'])) {
         $phpThumb->setParameter('sx', $settings['sx']);
         $phpThumb->setParameter('sy', $settings['sy']);
         $phpThumb->setParameter('sw', $settings['sw']);
         $phpThumb->setParameter('sh', $settings['sh']);
         if ($settings['aoe']) {
             $phpThumb->setParameter('aoe', 1);
         }
     }
     if (isset($settings['f'])) {
         $phpThumb->setParameter('f', $settings['f']);
     }
     if (!$phpThumb->generateThumbnail()) {
         /* Woopsy, the image couldn't be resized */
         debug($phpThumb->debugmessages);
         $this->errors[$Model->name][] = __('PhpThumb behavior : le fichier ', true) . $filepath . __(' n\'a pas pu être resizé', true);
         return false;
     }
     if (!$phpThumb->RenderToFile($settings['output'])) {
         /* File couldn't be created */
         $this->errors[$Model->name][] = __('PhpThumb behavior : le fichier ', true) . $settings['folder'] . $settings['output'] . __(' n\'a pas pu être créé', true);
         return false;
     }
     if ($settings['delete_source'] && $settings['output'] != $filepath) {
         if (!unlink($filepath)) {
             $this->errors[$Model->name][] = __('PhpThumb behavior : le fichier ', true) . $filepath . __(' n\'a pas pu être supprimé', true);
             return false;
         }
     }
     return true;
 }
 /**
  * Creation of an image thumbnail
  *
  * @param $name String File name on server
  * @param $filename String Final file name
  * @param $new_w Int Width in px
  * @param $new_h Int Height in px
  * @param $zc Bool "Zoom crop" (if true, cuts the largest possible square from the image center)
  */
 function createthumb($name, $filename, $new_w, $new_h, $zc)
 {
     App::import(array('file' => 'vendors' . DS . 'phpThumb' . DS . 'phpthumb.class.php', 'name' => 'MeioUpload.phpThumb', 'type' => 'file'));
     //mod_rewriteが使用できない場合にパスにindex.phpが入ってしまうため
     //絶対パスに変更
     if (Configure::read('App.baseUrl')) {
         $name = WWW_ROOT . $name;
         $filename = WWW_ROOT . $filename;
     }
     $phpThumb = new phpThumb();
     $phpThumb->config_allow_src_above_docroot = true;
     $phpThumb->setSourceFilename($name);
     $phpThumb->setParameter('w', $new_w);
     $phpThumb->setParameter('h', $new_h);
     $phpThumb->setParameter('zc', $zc);
     $parts = pathinfo($filename);
     //vd($parts);die();
     $phpThumb->setParameter('f', $parts['extension']);
     if ($phpThumb->generateThumbnail()) {
         $phpThumb->RenderToFile($filename);
     } else {
         die($phpThumb->fatalerror);
     }
 }
Ejemplo n.º 3
0
 /**
  * This is the method that actually does the thumbnail generation by setting up
  * the parameters and calling phpThumb
  *
  * @return bool Success?
  * @author Nate Constant
  **/
 function generateThumb($baseDir, $dir, $filename, $size, $options = array())
 {
     // Make sure we have the name of the uploaded file and that the Model is specified
     if (empty($baseDir) || empty($filename)) {
         Debugger::log('Base directory or filename is empty');
         return false;
     }
     $source = $baseDir . $dir . $filename;
     if (empty($size)) {
         $height = 100;
         $width = 100;
     } else {
         $height = $size['height'];
         $width = $size['width'];
     }
     // verify that the size is greater than 0 ( emtpy file uploaded )
     if (filesize($baseDir . $dir . $filename === 0)) {
         Debugger::log('File is empty');
         return false;
     }
     // verify that the filesystem is writable, if not add an error to the object
     // dont fail if not and let phpThumb try anyway
     $cacheDir = $baseDir . 'cache' . DS;
     if (!file_exists($cacheDir)) {
         debugger::log('Cache directory doesn\'t exist');
         if (!mkdir($cacheDir)) {
             Debugger::log('Can\' create ' . $cacheDir);
             return false;
         }
     }
     if (!is_writable($cacheDir)) {
         debugger::log($cacheDir . ' not writable');
         chmod($cacheDir, 0777);
     }
     // Load phpThumb
     App::import('Vendor', 'phpThumb', array('file' => 'phpThumb/phpthumb.class.php'));
     $phpThumb = new phpThumb();
     // phpThumb configs
     $phpThumb->setParameter('config_cache_force_passthru', false);
     $phpThumb->setParameter('config_allow_src_above_docroot', true);
     // ignore aspect ratio and allow enlarging
     //		$phpThumb->setParameter('iar', 1);
     // image configs
     $phpThumb->setSourceFilename($source);
     $phpThumb->setParameter('w', $width);
     $phpThumb->setParameter('h', $height);
     // auto rotate based on exif data
     $phpThumb->setParameter('ar', 'x');
     foreach ($options as $key => $val) {
         $phpThumb->setParameter($key, $val);
     }
     //		$phpThumb->setParameter('zc', 1);
     if ($phpThumb->generateThumbnail()) {
         if (!$phpThumb->RenderToFile($cacheDir . $filename . '-' . $height . 'x' . $width . '.jpg')) {
             Debugger::log('Could not render to file');
             return false;
         }
     } else {
         Debugger::log('Could not generate thumbnail');
         return false;
     }
     return true;
 }
 public function thumb($file, $dest_file = false)
 {
     if (empty($file) or !file_exists($file)) {
         return false;
     }
     /*
      * load phpThumb from vendors directory
      * and get a new instance
      */
     App::import('Vendor', 'phpThumb', array('file' => 'phpThumb' . DS . 'phpthumb.class.php'));
     $phpThumb = new phpThumb();
     // configure phpThumb for it's thumbnail generation
     $phpThumb->setSourceFilename($file);
     $phpThumb->setParameter('w', $this->width);
     $phpThumb->setParameter('h', $this->height);
     $phpThumb->setParameter('zc', $this->zoom_crop);
     /*
      * generate thumbnail
      * and render to file
      */
     $pathinfo = pathinfo($file);
     if ($dest_file) {
         $destination = $this->save_paths['thumb'] . $dest_file;
     } else {
         $destination = $this->save_paths['thumb'] . md5($pathinfo['filename'] . $this->width . $this->height . $this->zoom_crop) . '.' . $pathinfo['extension'];
     }
     /*
      * if their is an older version of the thumbnail
      * (same source, width, height, zoom-crop),
      * then delete
      */
     if (file_exists($destination)) {
         @unlink($destination);
     }
     if ($phpThumb->generateThumbnail() and $phpThumb->RenderToFile($destination)) {
         return $destination;
     } else {
         // something goes wrong
         return false;
     }
 }