/** * Returns url of thumbnail * * @param string $path Path to original file. Uses IMAGES as root path if path is relative * @param array $params Array of phpThumb parameters * @see LibPhpThumb::getThumbnailUrl() */ public function imageUrl($path, $params = array()) { if (preg_match('@\\:\\/\\/@', $path)) { return $path; } if ($path[0] != "/" && !preg_match('/^[A-Z]\\:\\\\/', $path)) { $path = IMAGES . $path; } try { list($thumbPath, $thumbUrl) = LibPhpThumb::getThumbnail($path, null, $params); return $thumbUrl; } catch (Exception $e) { debug($e->getMessage()); return false; } return $path; }
/** * 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; }
protected function _attachPreview($attachment, $config) { $preview = $config['preview']; //check if enabled in config if ($preview === false) { return $attachment; } //check if preview can be created for this file extension if (!$this->_validateFileExtension($attachment['ext'], array('jpg', 'jpeg', 'png', 'gif'))) { return $attachment; } //TODO validate mime type //if (!$this->_validateMimeType($attachment['type'], 'image/*')) // return $attachment; //default preview params if ($preview === true) { $preview = array(); $preview['default'] = array('width' => self::DEFAULT_PREVIEW_W, 'height' => self::DEFAULT_PREVIEW_H, 'quality' => self::DEFAULT_PREVIEW_Q); } // phpThumb-specific params mapping $paramsMap = array('width' => 'w', 'height' => 'h', 'quality' => 'q'); // create thumbs //TODO refactor to use an engine/interface instead of hardcoding with LibPhpThumb foreach ($preview as $size => $params) { // map preview params $_params = array(); foreach ($params as $k => $v) { if (array_key_exists($k, $paramsMap)) { $_params[$paramsMap[$k]] = $v; } else { $_params[$k] = $v; } } // reset values $path = $url = null; // get thumb try { $sourcePath = $attachment['path']; //TODO make the default image optional if (!file_exists($sourcePath)) { $sourcePath = str_replace("{DS}", DS, App::pluginPath('Media') . '{DS}webroot{DS}img{DS}attachments{DS}default.jpg'); } list($path, $url) = LibPhpThumb::getThumbnail($sourcePath, null, $_params); } catch (Exception $e) { #debug($e->getMessage()); $this->log('AttachableBehavior::_createPreview(): ' . $e->getMessage(), 'error'); } $attachment['preview'][$size] = compact('path', 'url'); } return $attachment; }
/** * 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; }