PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Media/Model/Image.php

https://bitbucket.org/jokusafet/magento2
PHP | 282 lines | 157 code | 44 blank | 81 comment | 23 complexity | 6212947d76b959f8c3539413e24e18bd MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Media
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Media library Image model
  28. *
  29. * @category Mage
  30. * @package Mage_Media
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Media_Model_Image extends Mage_Core_Model_Abstract
  34. {
  35. /**
  36. * Image config instance
  37. *
  38. * @var Mage_Media_Model_Image_Config_Interface
  39. */
  40. protected $_config;
  41. /**
  42. * Image resource
  43. *
  44. * @var resource
  45. */
  46. protected $_image;
  47. /**
  48. * Tmp image resource
  49. *
  50. * @var resource
  51. */
  52. protected $_tmpImage;
  53. /**
  54. * Params for filename generation
  55. *
  56. * @var array
  57. */
  58. protected $_params = array();
  59. protected function _construct()
  60. {
  61. $this->_init('Mage_Media_Model_File_Image');
  62. }
  63. /**
  64. * Set media image config instance
  65. *
  66. * @param Mage_Media_Model_Image_Config_Interface $config
  67. * @return unknown
  68. */
  69. public function setConfig(Mage_Media_Model_Image_Config_Interface $config)
  70. {
  71. $this->_config = $config;
  72. return $this;
  73. }
  74. /**
  75. * Retrive media image config instance
  76. *
  77. * @return Mage_Media_Model_Image_Config_Interface
  78. */
  79. public function getConfig()
  80. {
  81. return $this->_config;
  82. }
  83. public function getImage()
  84. {
  85. if(is_null($this->_image)) {
  86. $this->_image = $this->_getResource()->getImage($this);
  87. }
  88. return $this->_image;
  89. }
  90. public function getTmpImage()
  91. {
  92. if(is_null($this->_image)) {
  93. $this->_tmpImage = $this->_getResource()->getTmpImage($this);
  94. }
  95. return $this->_tmpImage;
  96. }
  97. /**
  98. * Retrive source dimensions object
  99. *
  100. * @return Varien_Object
  101. */
  102. public function getDimensions()
  103. {
  104. if(!$this->getData('dimensions')) {
  105. $this->setData('dimensions', $this->_getResource()->getDimensions($this));
  106. }
  107. return $this->getData('dimensions');
  108. }
  109. /**
  110. * Retrive destanation dimensions object
  111. *
  112. * @return Varien_Object
  113. */
  114. public function getDestanationDimensions()
  115. {
  116. if(!$this->getData('destanation_dimensions')) {
  117. $this->setData('destanation_dimensions', clone $this->getDimensions());
  118. }
  119. return $this->getData('destanation_dimensions');
  120. }
  121. public function getExtension()
  122. {
  123. return substr($this->getFileName(), strrpos($this->getFileName(), '.')+1);
  124. }
  125. public function getFilePath($useParams=false)
  126. {
  127. if($useParams && sizeof($this->getParams())) {
  128. $changes = '.' . $this->getParamsSum();
  129. } else {
  130. $changes = '';
  131. }
  132. return $this->getConfig()->getBaseMediaPath() . DS . $this->getName() . $changes . '.'
  133. . ( ( $useParams && $this->getParam('extension')) ? $this->getParam('extension') : $this->getExtension() );
  134. }
  135. public function getFileUrl($useParams=false)
  136. {
  137. if($useParams && sizeof($this->getParams())) {
  138. $changes = '.' . $this->getParamsSum();
  139. } else {
  140. $changes = '';
  141. }
  142. return $this->getConfig()->getBaseMediaUrl() . '/' . $this->getName() . $changes . '.'
  143. . ( ( $useParams && $this->getParam('extension')) ? $this->getParam('extension') : $this->getExtension() );
  144. }
  145. public function getName()
  146. {
  147. return substr($this->getFileName(), 0, strrpos($this->getFileName(), '.'));
  148. }
  149. public function addParam($param, $value=null)
  150. {
  151. if(is_array($param)) {
  152. $this->_params = array_merge($this->_params, $param);
  153. } else {
  154. $this->_params[$param] = $value;
  155. }
  156. return $this;
  157. }
  158. public function setParam($param, $value=null)
  159. {
  160. if(is_array($param)) {
  161. $this->_params = $param;
  162. } else {
  163. $this->_params[$param] = $value;
  164. }
  165. return $this;
  166. }
  167. public function getParam($param)
  168. {
  169. if(isset($this->_params[$param])) {
  170. return $this->_params[$param];
  171. }
  172. return null;
  173. }
  174. public function getParams()
  175. {
  176. return $this->_params;
  177. }
  178. public function getParamsSum()
  179. {
  180. return md5(serialize($this->_params));
  181. }
  182. /**
  183. * Return special link (with creating image if not exists)
  184. *
  185. * @param string $file
  186. * @param string $size
  187. * @param string $extension
  188. * @param string $watermark
  189. * @return string
  190. */
  191. public function getSpecialLink($file, $size, $extension=null, $watermark=null)
  192. {
  193. $this->_removeResources();
  194. $this->setData(array());
  195. $this->setParam(array());
  196. $this->setFileName($file);
  197. $this->addParam('size', $size);
  198. $this->addParam('watermark', $watermark);
  199. $this->addParam('extension', $extension);
  200. if(!$this->hasSpecialImage()) {
  201. if (strpos($size, 'x')!==false) {
  202. list($width, $height) = explode('x', $size);
  203. } else {
  204. $width = $size;
  205. $height = $this->getDimensions()->getHeight();
  206. }
  207. $sizeHRate = $width / $this->getDimensions()->getWidth();
  208. $sizeVRate = $height / $this->getDimensions()->getHeight();
  209. $rate = min($sizeHRate, $sizeVRate);
  210. if ($rate > 1) { // If image smaller than needed
  211. $rate = 1;
  212. }
  213. $this->getDestanationDimensions()
  214. ->setWidth($rate*$this->getDimensions()->getWidth())
  215. ->setHeight($rate*$this->getDimensions()->getHeight());
  216. $this->_getResource()->resize($this);
  217. $this->_getResource()->watermark($this);
  218. $this->_getResource()->saveAs($this, $extension);
  219. $this->_removeResources();
  220. }
  221. return $this->getFileUrl(true);
  222. }
  223. public function hasSpecialImage()
  224. {
  225. return $this->_getResource()->hasSpecialImage($this);
  226. }
  227. protected function _removeResources()
  228. {
  229. if ($this->_image) {
  230. $this->_getResource()->destroyResource($this->_image);
  231. $this->_image = null;
  232. }
  233. if ($this->_tmpImage) {
  234. $this->_getResource()->destroyResource($this->_tmpImage);
  235. $this->_tmpImage = null;
  236. }
  237. }
  238. }