/Controller/Component/PreviewManagerComponent.php
PHP | 137 lines | 95 code | 9 blank | 33 comment | 19 complexity | 37488f9375b34b3bf32e3414f33dd25e MD5 | raw file
- <?php
- /**
- * PHP versions 5
- *
- * phTagr : Organize, Browse, and Share Your Photos.
- * Copyright 2006-2013, Sebastian Felis (sebastian@phtagr.org)
- *
- * Licensed under The GPL-2.0 License
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright 2006-2013, Sebastian Felis (sebastian@phtagr.org)
- * @link http://www.phtagr.org phTagr
- * @package Phtagr
- * @since phTagr 2.2b3
- * @license GPL-2.0 (http://www.opensource.org/licenses/GPL-2.0)
- */
- class PreviewManagerComponent extends Component {
- var $controller = null;
- var $components = array('FileCache');
- /**
- * Defauls for a preview configuration
- */
- var $defaults = array(
- 'type' => 'image',
- 'square' => false,
- 'size' => 1280,
- 'requires' => false,
- 'extension' => 'jpg',
- 'quality' => 85,
- 'force' => false,
- 'clearMetaData' => false,
- 'rotation' => 0
- );
- /**
- * Different preview configurations
- */
- var $config = array(
- 'mini' => array(
- 'size' => OUTPUT_SIZE_MINI,
- 'square' => true,
- 'requires' => 'thumb'),
- 'thumb' => array(
- 'size' => OUTPUT_SIZE_THUMB,
- 'requires' => 'preview'),
- 'preview' => array(
- 'size' => OUTPUT_SIZE_PREVIEW,
- 'requires' => 'high'),
- 'high' => array(
- 'quality' => 90,
- 'size' => OUTPUT_SIZE_HIGH),
- 'hd' => array(
- 'size' => OUTPUT_SIZE_HD),
- );
- var $errors = 0;
- public function initialize(Controller $controller) {
- $this->controller = $controller;
- if (!isset($controller->Media)) {
- CakeLog::error("Model MyFile and Media is not found");
- return false;
- }
- }
- /**
- * Return image source file of the media
- */
- public function _getImageSoureFilename($media) {
- $type = $this->controller->Media->getType($media);
- if ($type != MEDIA_TYPE_IMAGE && $type != MEDIA_TYPE_VIDEO) {
- CakeLog::error("Media type not supported: {$this->controller->Media->getType($media)}");
- return false;
- }
- if ($type == MEDIA_TYPE_VIDEO) {
- $this->controller->loadComponent('VideoPreview', $this);
- return $this->VideoPreview->getPreviewFilename($media);
- }
- $file = $this->controller->Media->getFile($media, FILE_TYPE_IMAGE, false);
- if (!$file) {
- CakeLog::error("No files are attached to media {$media['Media']['id']}");
- return false;
- }
- return $this->controller->Media->File->getFilename($file);
- }
- /**
- * Fetches the preview of a given media.
- *
- * @param media Media model data
- * @param name Configuration name
- * @param config (Optional) configuration for the preview generation
- * @return Full path to the preview file
- */
- public function getPreview(&$media, $name, $config = array()) {
- $config = am($this->defaults, $config);
- if (isset($this->config[$name])) {
- $config = am($config, $this->config[$name]);
- }
- if ($config['requires']) {
- $src = $this->getPreview($media, $config['requires']);
- if (!$src) {
- CakeLog::error("Could not get preview of {$config['requires']}");
- return false;
- }
- } else {
- $src = $this->_getImageSoureFilename($media);
- $config['clearMetaData'] = true;
- $config['rotation'] = $this->controller->Media->getRotationInDegree($media);
- $config['isOriginal'] = true;
- }
- $dst = $this->FileCache->getFilePath($media, $name);
- if (!$dst) {
- CakeLog::error("Could not get cache file path for media {$this->controller->Media->toString($media)}");
- return false;
- }
- if (file_exists($dst) && !$config['force']) {
- if (is_readable($dst)) {
- return $dst;
- } else {
- CakeLog::error("Cachefile not readable: $dst");
- return false;
- }
- }
- $this->controller->loadComponent('ImageResizer', $this);
- if (!$this->ImageResizer->resize($src, $dst, $config)) {
- CakeLog::error("Resize of '$src' to '$dst' failed");
- //CakeLog::debug($config);
- return false;
- }
- return $dst;
- }
- }