PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Controller/Component/PreviewManagerComponent.php

https://github.com/xemle/phtagr
PHP | 137 lines | 95 code | 9 blank | 33 comment | 19 complexity | 37488f9375b34b3bf32e3414f33dd25e MD5 | raw file
  1. <?php
  2. /**
  3. * PHP versions 5
  4. *
  5. * phTagr : Organize, Browse, and Share Your Photos.
  6. * Copyright 2006-2013, Sebastian Felis (sebastian@phtagr.org)
  7. *
  8. * Licensed under The GPL-2.0 License
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright 2006-2013, Sebastian Felis (sebastian@phtagr.org)
  12. * @link http://www.phtagr.org phTagr
  13. * @package Phtagr
  14. * @since phTagr 2.2b3
  15. * @license GPL-2.0 (http://www.opensource.org/licenses/GPL-2.0)
  16. */
  17. class PreviewManagerComponent extends Component {
  18. var $controller = null;
  19. var $components = array('FileCache');
  20. /**
  21. * Defauls for a preview configuration
  22. */
  23. var $defaults = array(
  24. 'type' => 'image',
  25. 'square' => false,
  26. 'size' => 1280,
  27. 'requires' => false,
  28. 'extension' => 'jpg',
  29. 'quality' => 85,
  30. 'force' => false,
  31. 'clearMetaData' => false,
  32. 'rotation' => 0
  33. );
  34. /**
  35. * Different preview configurations
  36. */
  37. var $config = array(
  38. 'mini' => array(
  39. 'size' => OUTPUT_SIZE_MINI,
  40. 'square' => true,
  41. 'requires' => 'thumb'),
  42. 'thumb' => array(
  43. 'size' => OUTPUT_SIZE_THUMB,
  44. 'requires' => 'preview'),
  45. 'preview' => array(
  46. 'size' => OUTPUT_SIZE_PREVIEW,
  47. 'requires' => 'high'),
  48. 'high' => array(
  49. 'quality' => 90,
  50. 'size' => OUTPUT_SIZE_HIGH),
  51. 'hd' => array(
  52. 'size' => OUTPUT_SIZE_HD),
  53. );
  54. var $errors = 0;
  55. public function initialize(Controller $controller) {
  56. $this->controller = $controller;
  57. if (!isset($controller->Media)) {
  58. CakeLog::error("Model MyFile and Media is not found");
  59. return false;
  60. }
  61. }
  62. /**
  63. * Return image source file of the media
  64. */
  65. public function _getImageSoureFilename($media) {
  66. $type = $this->controller->Media->getType($media);
  67. if ($type != MEDIA_TYPE_IMAGE && $type != MEDIA_TYPE_VIDEO) {
  68. CakeLog::error("Media type not supported: {$this->controller->Media->getType($media)}");
  69. return false;
  70. }
  71. if ($type == MEDIA_TYPE_VIDEO) {
  72. $this->controller->loadComponent('VideoPreview', $this);
  73. return $this->VideoPreview->getPreviewFilename($media);
  74. }
  75. $file = $this->controller->Media->getFile($media, FILE_TYPE_IMAGE, false);
  76. if (!$file) {
  77. CakeLog::error("No files are attached to media {$media['Media']['id']}");
  78. return false;
  79. }
  80. return $this->controller->Media->File->getFilename($file);
  81. }
  82. /**
  83. * Fetches the preview of a given media.
  84. *
  85. * @param media Media model data
  86. * @param name Configuration name
  87. * @param config (Optional) configuration for the preview generation
  88. * @return Full path to the preview file
  89. */
  90. public function getPreview(&$media, $name, $config = array()) {
  91. $config = am($this->defaults, $config);
  92. if (isset($this->config[$name])) {
  93. $config = am($config, $this->config[$name]);
  94. }
  95. if ($config['requires']) {
  96. $src = $this->getPreview($media, $config['requires']);
  97. if (!$src) {
  98. CakeLog::error("Could not get preview of {$config['requires']}");
  99. return false;
  100. }
  101. } else {
  102. $src = $this->_getImageSoureFilename($media);
  103. $config['clearMetaData'] = true;
  104. $config['rotation'] = $this->controller->Media->getRotationInDegree($media);
  105. $config['isOriginal'] = true;
  106. }
  107. $dst = $this->FileCache->getFilePath($media, $name);
  108. if (!$dst) {
  109. CakeLog::error("Could not get cache file path for media {$this->controller->Media->toString($media)}");
  110. return false;
  111. }
  112. if (file_exists($dst) && !$config['force']) {
  113. if (is_readable($dst)) {
  114. return $dst;
  115. } else {
  116. CakeLog::error("Cachefile not readable: $dst");
  117. return false;
  118. }
  119. }
  120. $this->controller->loadComponent('ImageResizer', $this);
  121. if (!$this->ImageResizer->resize($src, $dst, $config)) {
  122. CakeLog::error("Resize of '$src' to '$dst' failed");
  123. //CakeLog::debug($config);
  124. return false;
  125. }
  126. return $dst;
  127. }
  128. }