PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/community/Fishpig/IBanners/Helper/Image.php

https://bitbucket.org/acidel/buykoala
PHP | 294 lines | 122 code | 36 blank | 136 comment | 14 complexity | 92c7b39b672a4b041b59ce7b2abe4216 MD5 | raw file
  1. <?php
  2. /**
  3. * @category Fishpig
  4. * @package Fishpig_iBanners
  5. * @license http://fishpig.co.uk/license.txt
  6. * @author Ben Tideswell <help@fishpig.co.uk>
  7. */
  8. class Fishpig_iBanners_Helper_Image extends Mage_Core_Helper_Abstract
  9. {
  10. /**
  11. * Stores version of generic image uploader/resizer
  12. *
  13. * @var const string
  14. */
  15. const VERSION_ID = '1.0.1';
  16. /**
  17. * Storeage for image object, used for resizing images
  18. *
  19. * @var null/Varien_Image
  20. */
  21. protected $_imageObject = null;
  22. /**
  23. * Flag used to determine wether to recreate already cached image
  24. *
  25. * @var bool
  26. */
  27. protected $_forceRecreate = false;
  28. /**
  29. * Filename currently initialized in the image object
  30. *
  31. * @var null|string
  32. */
  33. protected $_filename = '';
  34. /**
  35. * The folder name used to store images
  36. * This is relative to the media directory
  37. *
  38. * @var const string
  39. */
  40. const IMAGE_FOLDER = 'ibanners';
  41. /**
  42. * Retrieve the image URL where images are stored
  43. *
  44. * @return string
  45. */
  46. public function getBaseImageUrl()
  47. {
  48. return Mage::getBaseUrl('media') . self::IMAGE_FOLDER . '/';
  49. }
  50. /**
  51. * Retrieve the directory/path where images are stored
  52. *
  53. * @return string
  54. */
  55. public function getBaseImagePath()
  56. {
  57. return Mage::getBaseDir('media') . DS . self::IMAGE_FOLDER . DS;
  58. }
  59. /**
  60. * Retrieve the full image URL
  61. * Null returned if image does not exist
  62. *
  63. * @param string $image
  64. * @return string|null
  65. */
  66. public function getImageUrl($image)
  67. {
  68. if ($this->imageExists($image)) {
  69. return $this->getBaseImageUrl() . $image;
  70. }
  71. return null;
  72. }
  73. /**
  74. * Retrieve the full image path
  75. * Null returned if image does not exist
  76. *
  77. * @param string $image
  78. * @return string|null
  79. */
  80. public function getImagePath($image)
  81. {
  82. if ($this->imageExists($image)) {
  83. return $this->getBaseImagePath() . $image;
  84. }
  85. return null;
  86. }
  87. /**
  88. * determine whether the image exists
  89. *
  90. * @param string $image
  91. * @return bool
  92. */
  93. public function imageExists($image)
  94. {
  95. return is_file($this->getBaseImagePath() . $image);
  96. }
  97. /**
  98. * Converts a filename, width and height into it's resized uri path
  99. * returned path does not include base path
  100. *
  101. * @param string $filename
  102. * @param int $width = null
  103. * @param int $height = null
  104. * @return string
  105. */
  106. public function getResizedImageUrl($filename, $width = null, $height = null)
  107. {
  108. return $this->getBaseImageUrl() . $this->_getRelativeResizedImagePath($filename, $width, $height);
  109. }
  110. /**
  111. * Converts a filename, width and height into it's resized path
  112. * returned path does not include base path
  113. *
  114. * @param string $filename
  115. * @param int $width = null
  116. * @param int $height = null
  117. * @return string
  118. */
  119. public function getResizedImagePath($filename, $width = null, $height = null)
  120. {
  121. return $this->getBaseImagePath() . $this->_getRelativeResizedImagePath($filename, $width, $height);
  122. }
  123. /**
  124. * Converts a filename, width and height into it's resized path
  125. * returned path does not include base path
  126. *
  127. * @param string $filename
  128. * @param int $width = null
  129. * @param int $height = null
  130. * @return string
  131. */
  132. protected function _getRelativeResizedImagePath($filename, $width = null, $height = null)
  133. {
  134. if (!is_null($width) || !is_null($height)) {
  135. return 'cache' . DS . trim($width.'x'.$height, 'x') . DS . $filename;
  136. }
  137. return $filename;
  138. }
  139. /**
  140. * Initialize the image object
  141. * This sets up the image object for resizing and caching
  142. *
  143. * @param Fishpig_AttributeSplash_Model_Page $page
  144. * @param string $attribute
  145. * @return Fishpig_AttributeSplash_Helper_Image
  146. */
  147. public function init(Fishpig_AttributeSplash_Model_Page $page, $attribute = 'image')
  148. {
  149. $this->_imageObject = null;
  150. $this->_forceRecreate = false;
  151. $this->_filename = null;
  152. if ($imagePath = $this->getImagePath($page->getData($attribute))) {
  153. $this->_imageObject = new Varien_Image($imagePath);
  154. $this->_filename = basename($imagePath);
  155. $this->keepAspectRatio(true);
  156. }
  157. return $this;
  158. }
  159. /**
  160. * Resize the image loaded into the image object
  161. *
  162. * @param int $width = null
  163. * @param int $height = null
  164. * @return string
  165. */
  166. public function resize($width = null, $height = null)
  167. {
  168. if ($this->isActive()) {
  169. $cachedFilename = $this->getResizedImagePath($this->_filename, $width, $height);
  170. if ($this->_forceRecreate || !is_file($cachedFilename)) {
  171. $this->_imageObject->resize($width, $height);
  172. $this->_imageObject->save($cachedFilename);
  173. }
  174. return $this->getResizedImageUrl($this->_filename, $width, $height);;
  175. }
  176. return '';
  177. }
  178. /**
  179. * Keep the frame or add a white space
  180. *
  181. * @param bool $val
  182. */
  183. public function keepFrame($val)
  184. {
  185. if ($this->isActive()) {
  186. $this->_imageObject->keepFrame($val);
  187. }
  188. return $this;
  189. }
  190. /**
  191. * Keep the aspect ratio of an image
  192. *
  193. * @param bool $val
  194. */
  195. public function keepAspectRatio($val)
  196. {
  197. if ($this->isActive()) {
  198. $this->_imageObject->keepAspectRatio($val);
  199. }
  200. return $this;
  201. }
  202. /**
  203. * Don't increase the size of an image, only decrease
  204. *
  205. * @param bool $val
  206. */
  207. public function constrainOnly($val)
  208. {
  209. if ($this->isActive()) {
  210. $this->_imageObject->constrainOnly($val);
  211. }
  212. return $this;
  213. }
  214. /**
  215. * Determine whether to recreate image that already exists
  216. *
  217. * @param bool $val
  218. */
  219. public function forceRecreate($val)
  220. {
  221. if ($this->isActive()) {
  222. $this->_forceRecreate = $val;
  223. }
  224. return $this;
  225. }
  226. /**
  227. * Determine whether the image object has been initialised
  228. *
  229. * @return bool
  230. */
  231. public function isActive()
  232. {
  233. return is_object($this->_imageObject);
  234. }
  235. /**
  236. * Upload an image based on the $fileKey
  237. *
  238. * @param string $fileKey
  239. * @param string|null $filename - set a custom filename
  240. * @return null|string - returns saved filename
  241. */
  242. public function uploadImage($fileKey, $filename = null)
  243. {
  244. try {
  245. $uploader = new Varien_File_Uploader($fileKey);
  246. $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
  247. $uploader->setAllowRenameFiles(true);
  248. $result = $uploader->save($this->getBaseImagePath());
  249. return $result['file'];
  250. }
  251. catch (Exception $e) {
  252. if ($e->getCode() != Varien_File_Uploader::TMP_NAME_EMPTY) {
  253. throw $e;
  254. }
  255. }
  256. return null;
  257. }
  258. }