PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Helper/Image.php

https://github.com/gryzz/crystal_magento
PHP | 501 lines | 255 code | 47 blank | 199 comment | 15 complexity | cc7512f37b0eacb728ccdcb19c8061a0 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_Catalog
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Catalog image helper
  28. *
  29. * @author Magento Core Team <core@magentocommerce.com>
  30. */
  31. class Mage_Catalog_Helper_Image extends Mage_Core_Helper_Abstract
  32. {
  33. protected $_model;
  34. protected $_scheduleResize = false;
  35. protected $_scheduleRotate = false;
  36. protected $_angle;
  37. protected $_watermark;
  38. protected $_watermarkPosition;
  39. protected $_watermarkSize;
  40. protected $_watermarkImageOpacity;
  41. protected $_product;
  42. protected $_imageFile;
  43. protected $_placeholder;
  44. /**
  45. * Reset all previos data
  46. */
  47. protected function _reset()
  48. {
  49. $this->_model = null;
  50. $this->_scheduleResize = false;
  51. $this->_scheduleRotate = false;
  52. $this->_angle = null;
  53. $this->_watermark = null;
  54. $this->_watermarkPosition = null;
  55. $this->_watermarkSize = null;
  56. $this->_watermarkImageOpacity = null;
  57. $this->_product = null;
  58. $this->_imageFile = null;
  59. return $this;
  60. }
  61. public function init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)
  62. {
  63. $this->_reset();
  64. $this->_setModel(Mage::getModel('catalog/product_image'));
  65. $this->_getModel()->setDestinationSubdir($attributeName);
  66. $this->setProduct($product);
  67. $this->setWatermark(Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_image"));
  68. $this->setWatermarkImageOpacity(Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_imageOpacity"));
  69. $this->setWatermarkPosition(Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_position"));
  70. $this->setWatermarkSize(Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_size"));
  71. if ($imageFile) {
  72. $this->setImageFile($imageFile);
  73. }
  74. else {
  75. // add for work original size
  76. $this->_getModel()->setBaseFile( $this->getProduct()->getData($this->_getModel()->getDestinationSubdir()) );
  77. }
  78. return $this;
  79. }
  80. /**
  81. * Schedule resize of the image
  82. * $width *or* $height can be null - in this case, lacking dimension will be calculated.
  83. *
  84. * @see Mage_Catalog_Model_Product_Image
  85. * @param int $width
  86. * @param int $height
  87. * @return Mage_Catalog_Helper_Image
  88. */
  89. public function resize($width, $height = null)
  90. {
  91. $this->_getModel()->setWidth($width)->setHeight($height);
  92. $this->_scheduleResize = true;
  93. return $this;
  94. }
  95. /**
  96. * Set image quality, values in percentage from 0 to 100
  97. *
  98. * @param int $quality
  99. * @return Mage_Catalog_Helper_Image
  100. */
  101. public function setQuality($quality)
  102. {
  103. $this->_getModel()->setQuality($quality);
  104. return $this;
  105. }
  106. /**
  107. * Guarantee, that image picture width/height will not be distorted.
  108. * Applicable before calling resize()
  109. * It is true by default.
  110. *
  111. * @see Mage_Catalog_Model_Product_Image
  112. * @param bool $flag
  113. * @return Mage_Catalog_Helper_Image
  114. */
  115. public function keepAspectRatio($flag)
  116. {
  117. $this->_getModel()->setKeepAspectRatio($flag);
  118. return $this;
  119. }
  120. /**
  121. * Guarantee, that image will have dimensions, set in $width/$height
  122. * Applicable before calling resize()
  123. * Not applicable, if keepAspectRatio(false)
  124. *
  125. * $position - TODO, not used for now - picture position inside the frame.
  126. *
  127. * @see Mage_Catalog_Model_Product_Image
  128. * @param bool $flag
  129. * @param array $position
  130. * @return Mage_Catalog_Helper_Image
  131. */
  132. public function keepFrame($flag, $position = array('center', 'middle'))
  133. {
  134. $this->_getModel()->setKeepFrame($flag);
  135. return $this;
  136. }
  137. /**
  138. * Guarantee, that image will not lose transparency if any.
  139. * Applicable before calling resize()
  140. * It is true by default.
  141. *
  142. * $alphaOpacity - TODO, not used for now
  143. *
  144. * @see Mage_Catalog_Model_Product_Image
  145. * @param bool $flag
  146. * @param int $alphaOpacity
  147. * @return Mage_Catalog_Helper_Image
  148. */
  149. public function keepTransparency($flag, $alphaOpacity = null)
  150. {
  151. $this->_getModel()->setKeepTransparency($flag);
  152. return $this;
  153. }
  154. /**
  155. * Guarantee, that image picture will not be bigger, than it was.
  156. * Applicable before calling resize()
  157. * It is false by default
  158. *
  159. * @param bool $flag
  160. * @return Mage_Catalog_Helper_Image
  161. */
  162. public function constrainOnly($flag)
  163. {
  164. $this->_getModel()->setConstrainOnly($flag);
  165. return $this;
  166. }
  167. /**
  168. * Set color to fill image frame with.
  169. * Applicable before calling resize()
  170. * The keepTransparency(true) overrides this (if image has transparent color)
  171. * It is white by default.
  172. *
  173. * @see Mage_Catalog_Model_Product_Image
  174. * @param array $colorRGB
  175. * @return Mage_Catalog_Helper_Image
  176. */
  177. public function backgroundColor($colorRGB)
  178. {
  179. // assume that 3 params were given instead of array
  180. if (!is_array($colorRGB)) {
  181. $colorRGB = func_get_args();
  182. }
  183. $this->_getModel()->setBackgroundColor($colorRGB);
  184. return $this;
  185. }
  186. public function rotate($angle)
  187. {
  188. $this->setAngle($angle);
  189. $this->_getModel()->setAngle($angle);
  190. $this->_scheduleRotate = true;
  191. return $this;
  192. }
  193. /**
  194. * Add watermark to image
  195. * size param in format 100x200
  196. *
  197. * @param string $fileName
  198. * @param string $position
  199. * @param string $size
  200. * @param int $imageOpacity
  201. * @return Mage_Catalog_Helper_Image
  202. */
  203. public function watermark($fileName, $position, $size=null, $imageOpacity=null)
  204. {
  205. $this->setWatermark($fileName)
  206. ->setWatermarkPosition($position)
  207. ->setWatermarkSize($size)
  208. ->setWatermarkImageOpacity($imageOpacity);
  209. return $this;
  210. }
  211. public function placeholder($fileName)
  212. {
  213. $this->_placeholder = $fileName;
  214. }
  215. public function getPlaceholder()
  216. {
  217. if (!$this->_placeholder) {
  218. $attr = $this->_getModel()->getDestinationSubdir();
  219. $this->_placeholder = 'images/catalog/product/placeholder/'.$attr.'.jpg';
  220. }
  221. return $this->_placeholder;
  222. }
  223. public function __toString()
  224. {
  225. try {
  226. if( $this->getImageFile() ) {
  227. $this->_getModel()->setBaseFile( $this->getImageFile() );
  228. } else {
  229. $this->_getModel()->setBaseFile( $this->getProduct()->getData($this->_getModel()->getDestinationSubdir()) );
  230. }
  231. if( $this->_getModel()->isCached() ) {
  232. return $this->_getModel()->getUrl();
  233. } else {
  234. if( $this->_scheduleRotate ) {
  235. $this->_getModel()->rotate( $this->getAngle() );
  236. }
  237. if ($this->_scheduleResize) {
  238. $this->_getModel()->resize();
  239. }
  240. if( $this->getWatermark() ) {
  241. $this->_getModel()->setWatermark($this->getWatermark());
  242. }
  243. $url = $this->_getModel()->saveFile()->getUrl();
  244. }
  245. } catch( Exception $e ) {
  246. $url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
  247. }
  248. return $url;
  249. }
  250. /**
  251. * Enter description here...
  252. *
  253. * @return Mage_Catalog_Helper_Image
  254. */
  255. protected function _setModel($model)
  256. {
  257. $this->_model = $model;
  258. return $this;
  259. }
  260. /**
  261. * Enter description here...
  262. *
  263. * @return Mage_Catalog_Model_Product_Image
  264. */
  265. protected function _getModel()
  266. {
  267. return $this->_model;
  268. }
  269. protected function setAngle($angle)
  270. {
  271. $this->_angle = $angle;
  272. return $this;
  273. }
  274. protected function getAngle()
  275. {
  276. return $this->_angle;
  277. }
  278. /**
  279. * Set watermark file name
  280. *
  281. * @param string $watermark
  282. * @return Mage_Catalog_Helper_Image
  283. */
  284. protected function setWatermark($watermark)
  285. {
  286. $this->_watermark = $watermark;
  287. $this->_getModel()->setWatermarkFile($watermark);
  288. return $this;
  289. }
  290. /**
  291. * Get watermark file name
  292. *
  293. * @return string
  294. */
  295. protected function getWatermark()
  296. {
  297. return $this->_watermark;
  298. }
  299. /**
  300. * Set watermark position
  301. *
  302. * @param string $position
  303. * @return Mage_Catalog_Helper_Image
  304. */
  305. protected function setWatermarkPosition($position)
  306. {
  307. $this->_watermarkPosition = $position;
  308. $this->_getModel()->setWatermarkPosition($position);
  309. return $this;
  310. }
  311. /**
  312. * Get watermark position
  313. *
  314. * @return string
  315. */
  316. protected function getWatermarkPosition()
  317. {
  318. return $this->_watermarkPosition;
  319. }
  320. /**
  321. * Set watermark size
  322. * param size in format 100x200
  323. *
  324. * @param string $size
  325. * @return Mage_Catalog_Helper_Image
  326. */
  327. public function setWatermarkSize($size)
  328. {
  329. $this->_watermarkSize = $size;
  330. $this->_getModel()->setWatermarkSize($this->parseSize($size));
  331. return $this;
  332. }
  333. /**
  334. * Get watermark size
  335. *
  336. * @return string
  337. */
  338. protected function getWatermarkSize()
  339. {
  340. return $this->_watermarkSize;
  341. }
  342. /**
  343. * Set watermark image opacity
  344. *
  345. * @param int $imageOpacity
  346. * @return Mage_Catalog_Helper_Image
  347. */
  348. public function setWatermarkImageOpacity($imageOpacity)
  349. {
  350. $this->_watermarkImageOpacity = $imageOpacity;
  351. $this->_getModel()->setWatermarkImageOpacity($imageOpacity);
  352. return $this;
  353. }
  354. /**
  355. * Get watermark image opacity
  356. *
  357. * @return int
  358. */
  359. protected function getWatermarkImageOpacity()
  360. {
  361. if( $this->_watermarkImageOpacity ) {
  362. return $this->_watermarkImageOpacity;
  363. }
  364. return $this->_getModel()->getWatermarkImageOpacity();
  365. }
  366. protected function setProduct($product)
  367. {
  368. $this->_product = $product;
  369. return $this;
  370. }
  371. protected function getProduct()
  372. {
  373. return $this->_product;
  374. }
  375. protected function setImageFile($file)
  376. {
  377. $this->_imageFile = $file;
  378. return $this;
  379. }
  380. protected function getImageFile()
  381. {
  382. return $this->_imageFile;
  383. }
  384. /**
  385. * Enter description here...
  386. *
  387. * @return array
  388. */
  389. protected function parseSize($string)
  390. {
  391. $size = explode('x', strtolower($string));
  392. if( sizeof($size) == 2 ) {
  393. return array(
  394. 'width' => ($size[0] > 0) ? $size[0] : null,
  395. 'heigth' => ($size[1] > 0) ? $size[1] : null,
  396. );
  397. }
  398. return false;
  399. }
  400. /**
  401. * Retrieve original image width
  402. *
  403. * @return int|null
  404. */
  405. public function getOriginalWidth()
  406. {
  407. return $this->_getModel()->getImageProcessor()->getOriginalWidth();
  408. }
  409. /**
  410. * Retrieve original image height
  411. *
  412. * @deprecated
  413. * @return int|null
  414. */
  415. public function getOriginalHeigh()
  416. {
  417. return $this->getOriginalHeight();
  418. }
  419. /**
  420. * Retrieve original image height
  421. *
  422. * @return int|null
  423. */
  424. public function getOriginalHeight()
  425. {
  426. return $this->_getModel()->getImageProcessor()->getOriginalHeight();
  427. }
  428. /**
  429. * Retrieve Original image size as array
  430. * 0 - width, 1 - height
  431. *
  432. * @return array
  433. */
  434. public function getOriginalSizeArray()
  435. {
  436. return array(
  437. $this->getOriginalWidth(),
  438. $this->getOriginalHeight()
  439. );
  440. }
  441. /**
  442. * Check - is this file an image
  443. *
  444. * @param string $filePath
  445. * @return bool
  446. * @throw Mage_Core_Exception
  447. */
  448. public function validateUploadFile($filePath) {
  449. if (!getimagesize($filePath)) {
  450. Mage::throwException($this->__('Disallowed file type.'));
  451. }
  452. return true;
  453. }
  454. }