示例#1
0
 /**
  * Attach Thumb path and url info to data row
  * 
  * @param Model $model
  * @param array $data
  * @param string $field
  * @throws CakeException
  * @return void
  */
 public function attachThumb(&$model, &$data, $field = 'image')
 {
     $s =& $this->settings[$model->alias];
     if (!isset($data[$model->alias][$field])) {
         return;
     }
     if (empty($data[$model->alias][$field])) {
         if (!$s['defaultImage']) {
             return;
         }
         $data[$model->alias][$field] = $s['defaultImage'];
     }
     //config
     if (!isset($this->thumbs[$model->alias][$field])) {
         if (Configure::read('debug') > 0) {
             throw new CakeException(__d('media', "ThumbBehavior::attachThumb() Thumb configuration for field '%s' is not set"));
         }
         $thumbs = array('default' => array('w' => 100, 'h' => 100, 'q' => 70));
     } else {
         $thumbs = $this->thumbs[$model->alias][$field];
     }
     //render thumbs and return paths
     $_thumbData = array();
     foreach ($thumbs as $name => $config) {
         $source = $source_url = $error = $thumb = $url = $url_full = null;
         try {
             //image source
             $file = $data[$model->alias][$field];
             if (!file_exists(IMAGES . $s['baseDir'] . $file)) {
                 if ($s['defaultImage']) {
                     //use default image if source not found
                     $file = $s['defaultImage'];
                 } else {
                     throw new CakeException(__d('media', "Source file '%s' does not exist", $source));
                 }
             }
             $source = IMAGES . $s['baseDir'] . $file;
             $source_url = Router::url('/', true) . IMAGES_URL . $s['baseDir'] . $file;
             //thumb-path
             $thumb = LibPhpThumb::getThumbnail($source, null, $config);
             //thumb-url
             $url = LibPhpThumb::getThumbnailUrlFromPath($thumb, false);
             $url_full = LibPhpThumb::getThumbnailUrlFromPath($thumb, true);
         } catch (Exception $e) {
             debug($e->getMessage());
             CakeLog::write('error', __d('media', "ThumbBehavior::attachThumb() [Field '%s';Thumb '%s']: %s", $field, $name, $e->getMessage()));
             $error = $e->getMessage();
         }
         $_thumbData[$name] = compact('source', 'source_url', 'thumb', 'url', 'url_full', 'error');
     }
     $data[$s['entity']][$field] = $_thumbData;
 }
示例#2
0
 /**
  * Attach Thumb path and url info to data row
  * 
  * @param Model $model
  * @param array $data
  * @param string $field
  * @throws CakeException
  * @return void
  */
 public function attachThumb(&$model, &$data, $field = 'image')
 {
     $s =& $this->settings[$model->alias];
     if (!isset($data[$model->alias][$field]) || empty($data[$model->alias][$field])) {
         if (!$s['defaultImage']) {
             return;
         }
         $data[$model->alias][$field] = null;
     }
     if (isset($data[$model->alias][$model->primaryKey])) {
         $model->id = $data[$model->alias][$model->primaryKey];
     }
     //config
     if (!isset($this->images[$model->alias][$field])) {
         if (Configure::read('debug') > 0) {
             throw new CakeException(__d('media', "ImageBehavior::attachThumb() Thumb configuration for field '%s' is not set", $field));
         }
         $images = array('default' => array('w' => 100, 'h' => 100, 'q' => 70));
     } else {
         $images = $this->images[$model->alias][$field];
     }
     //render images and return paths
     $_baseDir = $this->_replaceTokens($model, $s['baseDir']);
     $_imageData = array();
     foreach ($images as $name => $config) {
         $source = $source_url = $error = $image = $url = $url_full = null;
         try {
             //image source
             $file = $data[$model->alias][$field] ? $_baseDir . $data[$model->alias][$field] : $s['defaultImage'];
             $source = IMAGES . $file;
             //TODO refactor with do-loop and support more fallbacks for default images
             if (!file_exists($source)) {
                 //log that a source file is missing
                 CakeLog::write('error', __d('media', '[ImageBehavior] Image file \'%s\' is missing', $source));
                 if ($s['defaultImage']) {
                     //TODO default images can only be located in app webroot. enable plugin support
                     $source = IMAGES . $s['defaultImage'];
                 } else {
                     throw new CakeException(__d('media', "Source file '%s' does not exist", $source));
                 }
             }
             $source_url = Router::url('/', true) . IMAGES_URL . $file;
             //image-path
             $image = LibPhpThumb::getThumbnail($source, null, $config);
             //image-url
             $url = LibPhpThumb::getThumbnailUrlFromPath($image, false);
             $url_full = LibPhpThumb::getThumbnailUrlFromPath($image, true);
         } catch (Exception $e) {
             CakeLog::write('error', "[ImageBehavior][Field '%s';Thumb '%s']: AttachThumb failed: %s", $field, $name, $e->getMessage());
             $error = $e->getMessage();
         }
         $_imageData[$name] = compact('source', 'source_url', 'image', 'url', 'url_full', 'error');
     }
     $data[$s['entity']][$field] = $_imageData;
 }