PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_roksprocket/lib/RokSprocket/Item/Image.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 254 lines | 148 code | 31 blank | 75 comment | 20 complexity | b751de262093bbf27f932ba01cbb3079 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2013 RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. class RokSprocket_Item_Image
  9. {
  10. /**
  11. * @var string
  12. */
  13. protected $identifier;
  14. /**
  15. * @var string
  16. */
  17. protected $source;
  18. /**
  19. * @var string
  20. */
  21. protected $height = '100%';
  22. /**
  23. * @var string
  24. */
  25. protected $width = '100%';
  26. /**
  27. * @var string
  28. */
  29. protected $caption;
  30. /**
  31. * @var string
  32. */
  33. protected $alttext;
  34. /**
  35. * @var string
  36. */
  37. protected $filepath;
  38. /**
  39. * @static
  40. *
  41. * @param $json
  42. *
  43. * @return bool|null|RokSprocket_Item_Image
  44. */
  45. public static function createFromJSON($json)
  46. {
  47. $container = RokCommon_Service::getContainer();
  48. /** @var $logger RokCommon_Logger */
  49. $logger = $container->roksprocket_logger;
  50. $image = null;
  51. if (!empty($json)) {
  52. $image = new self();
  53. $json_decode = null;
  54. try {
  55. $json_decode = RokCommon_JSON::decode(str_replace("'", '"', str_replace('\\', '', $json)));
  56. } catch (RokCommon_JSON_Exception $jse) {
  57. $logger->warning("Unable to decode image JSON : " . $json);
  58. }
  59. if (!$json_decode) return false;
  60. $image->source = $json_decode->path;
  61. $size = array(0, 0);
  62. $image_source = $json_decode->path;
  63. if (!$container->platforminfo->isLinkExternal($image_source)) {
  64. $size_check_path = $container->platforminfo->getPathForUrl($image_source);
  65. $image->filepath = $size_check_path;
  66. /// if the image source doesnt start with / assume its relative to root
  67. } else {
  68. $size_check_path = $image_source;
  69. }
  70. if (!empty($image->filepath) && file_exists($image->filepath)) {
  71. $size = @getimagesize($size_check_path);
  72. if (!empty($size)) {
  73. $size[0] = ($size[0]) ? $size[0] : '100%';
  74. $size[1] = ($size[1]) ? $size[1] : '100%';
  75. }
  76. $image->width = $size[0];
  77. $image->height = $size[1];
  78. }
  79. }
  80. return $image;
  81. }
  82. protected function setSizeFromSource()
  83. {
  84. $container = RokCommon_Service::getContainer();
  85. $image_source = $this->source;
  86. if (!$container->platforminfo->isLinkExternal($image_source)) {
  87. $size_check_path = $container->platforminfo->getPathForUrl($image_source);
  88. $this->filepath = $size_check_path;
  89. /// if the image source doesnt start with / assume its relative to root
  90. } else {
  91. $size_check_path = $image_source;
  92. }
  93. if (!empty($this->filepath) && file_exists($this->filepath)) {
  94. $size = @getimagesize($size_check_path);
  95. if ($size !== false) {
  96. $this->width = $size[0];
  97. $this->height = $size[1];
  98. }
  99. }
  100. }
  101. /**
  102. * @param $height
  103. */
  104. public function setHeight($height)
  105. {
  106. $this->height = $height;
  107. }
  108. /**
  109. * @return mixed
  110. */
  111. public function getHeight()
  112. {
  113. return $this->height;
  114. }
  115. /**
  116. * @param $url
  117. * @param bool $autosetsize
  118. */
  119. public function setSource($url, $autosetsize = true)
  120. {
  121. $container = RokCommon_Service::getContainer();
  122. if (!$container->platforminfo->isLinkExternal($url)) {
  123. $url = $container->platforminfo->getUrlForPath($container->platforminfo->getPathForUrl($url));
  124. }
  125. $this->source = $url;
  126. if ($autosetsize) {
  127. $this->setSizeFromSource();
  128. }
  129. }
  130. /**
  131. * @return mixed
  132. */
  133. public function getSource()
  134. {
  135. return $this->source;
  136. }
  137. /**
  138. * @param $width
  139. */
  140. public function setWidth($width)
  141. {
  142. $this->width = $width;
  143. }
  144. /**
  145. * @return mixed
  146. */
  147. public function getWidth()
  148. {
  149. return $this->width;
  150. }
  151. /**
  152. * @param $identifier
  153. */
  154. public function setIdentifier($identifier)
  155. {
  156. $this->identifier = $identifier;
  157. }
  158. /**
  159. * @return mixed
  160. */
  161. public function getIdentifier()
  162. {
  163. return $this->identifier;
  164. }
  165. /**
  166. * @param $alttext
  167. */
  168. public function setAlttext($alttext)
  169. {
  170. $this->alttext = $alttext;
  171. }
  172. /**
  173. * @return mixed
  174. */
  175. public function getAlttext()
  176. {
  177. return $this->alttext;
  178. }
  179. /**
  180. * @param $caption
  181. */
  182. public function setCaption($caption)
  183. {
  184. $this->caption = $caption;
  185. }
  186. /**
  187. * @return mixed
  188. */
  189. public function getCaption()
  190. {
  191. return $this->caption;
  192. }
  193. public function resize($width = 0, $height = 0)
  194. {
  195. $container = RokCommon_Service::getContainer();
  196. /** @var $platformHelper RokSprocket_PlatformHelper */
  197. $platformHelper = $container->roksprocket_platformhelper;
  198. if (!empty($this->filepath) && file_exists($this->filepath)) {
  199. try {
  200. $file_parts = pathinfo($this->filepath);
  201. $file_path_md5 = md5($this->filepath);
  202. $new_path = sprintf('%s/%s_%d_%d.%s', $platformHelper->getCacheDir(), $file_path_md5, $height, $width, $file_parts['extension']);
  203. $new_source = sprintf('%s/%s_%d_%d.%s', $platformHelper->getCacheUrl(), $file_path_md5, $height, $width, $file_parts['extension']);
  204. if (!file_exists($new_path)) {
  205. $new_path_parts = pathinfo($new_path);
  206. if (!is_dir($new_path_parts['dirname']) && !is_file($new_path_parts['dirname'])) {
  207. $origmask = @umask(0);
  208. if (!$ret = @mkdir($new_path_parts['dirname'], 0755, true)) {
  209. @umask($origmask);
  210. throw new RokCommon_Image_Exception(rc__('Unable to create directory %s.', $new_path_parts['dirname']));
  211. }
  212. @umask($origmask);
  213. }
  214. $resized_image = RokCommon_Image::create($this->filepath);
  215. $resized_image->setOption(RokCommon_Image_ImageType::OPTION_NAME_RESIZE_UP, true);
  216. $resized_image->resize($width, $height)->save($new_path);
  217. }
  218. $this->setSource($new_source);
  219. } catch (RokCommon_Image_Exception $rcie) {
  220. }
  221. }
  222. }
  223. }