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

/library/Zend/View/Helper/TinySrc.php

https://gitlab.com/devtoannh/cafe
PHP | 317 lines | 139 code | 29 blank | 149 comment | 19 complexity | 2e35adb23f428b06bcb9ef56b4b6e786 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  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@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_View_Helper_HtmlElement */
  22. require_once 'Zend/View/Helper/HtmlElement.php';
  23. /**
  24. * Helper for generating urls and/or image tags for use with tinysrc.net
  25. *
  26. * tinysrc.net provides an API for generating scaled, browser device-specific
  27. * images. In essence, you pass the API the URL to an image on your own server,
  28. * and tinysrc.net then provides the appropriate image based on the device that
  29. * accesses it.
  30. *
  31. * Additionally, tinysrc.net allows you to specify additional configuration via
  32. * the API:
  33. *
  34. * - image size. You may define this as:
  35. * - explicit size
  36. * - subtractive size (size of screen minus specified number of pixels)
  37. * - percentage size (percentage of screen size))
  38. * - image format. This will convert the image to the given format; allowed
  39. * values are "png" or "jpeg". By default, gif images are converted to png.
  40. *
  41. * This helper allows you to specify all configuration options, as well as:
  42. *
  43. * - whether or not to generate the full image tag (or just the URL)
  44. * - base url to images (which should include the protocol, server, and
  45. * optionally port and base path)
  46. *
  47. * @see http://tinysrc.net/
  48. * @package Zend_View
  49. * @subpackage Helper
  50. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  51. * @license http://framework.zend.com/license/new-bsd New BSD License
  52. */
  53. class Zend_View_Helper_TinySrc extends Zend_View_Helper_HtmlElement
  54. {
  55. const TINYSRC_BASE = 'http://i.tinysrc.mobi';
  56. /**
  57. * @var string Base URL for images
  58. */
  59. protected $_baseUrl;
  60. /**
  61. * @var bool Whether or not to create an image tag
  62. */
  63. protected $_createTagFlag = true;
  64. /**
  65. * @var string Default width and height
  66. */
  67. protected $_dimensions = '';
  68. /**
  69. * Default options
  70. *
  71. * Used when determining what options were passed, and needing to merge
  72. * them with default options.
  73. *
  74. * @var array
  75. */
  76. protected $_defaultOptions = array(
  77. 'base_url' => null,
  78. 'format' => null,
  79. 'width' => false,
  80. 'height' => false,
  81. 'create_tag' => true,
  82. );
  83. /**
  84. * @var string Default image format to use
  85. */
  86. protected $_format = '';
  87. /**
  88. * Generate a link or image tag pointing to tinysrc.net
  89. *
  90. * @param mixed $image
  91. * @param array $options
  92. * @return void
  93. */
  94. public function tinySrc($image = null, array $options = array())
  95. {
  96. if (null === $image) {
  97. return $this;
  98. }
  99. $defaultOptions = $this->_defaultOptions;
  100. $defaultOptions['create_tag'] = $this->createTag();
  101. $options = array_merge($defaultOptions, $options);
  102. $url = '/' . $this->_mergeBaseUrl($options) . ltrim($image, '/');
  103. $src = self::TINYSRC_BASE
  104. . $this->_mergeFormat($options)
  105. . $this->_mergeDimensions($options)
  106. . $url;
  107. if (!$options['create_tag']) {
  108. return $src;
  109. }
  110. foreach (array_keys($this->_defaultOptions) as $key) {
  111. switch ($key) {
  112. case 'width':
  113. case 'height':
  114. if (!is_int($options[$key]) || !is_numeric($options[$key]) || $options[$key] < 0) {
  115. unset($options[$key]);
  116. }
  117. break;
  118. default:
  119. unset($options[$key]);
  120. break;
  121. }
  122. }
  123. $options['src'] = $src;
  124. $tag = '<img' . $this->_htmlAttribs($options) . $this->getClosingBracket();
  125. return $tag;
  126. }
  127. /**
  128. * Set base URL for images
  129. *
  130. * @param string $url
  131. * @return Zend_View_Helper_TinySrc
  132. */
  133. public function setBaseUrl($url)
  134. {
  135. $this->_baseUrl = rtrim($url, '/') . '/';
  136. return $this;
  137. }
  138. /**
  139. * Get base URL for images
  140. *
  141. * If none already set, uses the ServerUrl and BaseUrl view helpers to
  142. * determine the base URL to images.
  143. *
  144. * @return string
  145. */
  146. public function getBaseUrl()
  147. {
  148. if (null === $this->_baseUrl) {
  149. $this->setBaseUrl($this->view->serverUrl($this->view->baseUrl()));
  150. }
  151. return $this->_baseUrl;
  152. }
  153. /**
  154. * Set default image format
  155. *
  156. * If set, this will set the default format to use on all images.
  157. *
  158. * @param null|string $format
  159. * @return Zend_View_Helper_TinySrc
  160. * @throws Zend_View_Exception
  161. */
  162. public function setDefaultFormat($format = null)
  163. {
  164. if (null === $format) {
  165. $this->_format = '';
  166. return $this;
  167. }
  168. $format = strtolower($format);
  169. if (!in_array($format, array('png', 'jpeg'))) {
  170. require_once 'Zend/View/Exception.php';
  171. throw new Zend_View_Exception('Invalid format; must be one of "jpeg" or "png"');
  172. }
  173. $this->_format = "/$format";
  174. return $this;
  175. }
  176. /**
  177. * Set default dimensions
  178. *
  179. * If null is specified for width, default dimensions will be cleared. If
  180. * only width is specified, only width will be used. If either dimension
  181. * fails validation, an exception is raised.
  182. *
  183. * @param null|int|string $width
  184. * @param null|int|string $height
  185. * @return Zend_View_Helper_TinySrc
  186. * @throws Zend_View_Exception
  187. */
  188. public function setDefaultDimensions($width = null, $height = null)
  189. {
  190. if (null === $width) {
  191. $this->_dimensions = '';
  192. return $this;
  193. }
  194. if (!$this->_validateDimension($width)) {
  195. require_once 'Zend/View/Exception.php';
  196. throw new Zend_View_Exception('Invalid dimension; must be an integer, optionally preceded by "-" or "x"');
  197. }
  198. $this->_dimensions = "/$width";
  199. if (null === $height) {
  200. return $this;
  201. }
  202. if (!$this->_validateDimension($height)) {
  203. require_once 'Zend/View/Exception.php';
  204. throw new Zend_View_Exception('Invalid dimension; must be an integer, optionally preceded by "-" or "x"');
  205. }
  206. $this->_dimensions .= "/$height";
  207. return $this;
  208. }
  209. /**
  210. * Set state of "create tag" flag
  211. *
  212. * @param bool $flag
  213. * @return Zend_View_Helper_TinySrc
  214. */
  215. public function setCreateTag($flag)
  216. {
  217. $this->_createTagFlag = (bool) $flag;
  218. return $this;
  219. }
  220. /**
  221. * Should the helper create an image tag?
  222. *
  223. * @return bool
  224. */
  225. public function createTag()
  226. {
  227. return $this->_createTagFlag;
  228. }
  229. /**
  230. * Validate a dimension
  231. *
  232. * Dimensions may be integers, optionally preceded by '-' or 'x'.
  233. *
  234. * @param string $dim
  235. * @return bool
  236. */
  237. protected function _validateDimension($dim)
  238. {
  239. if (!is_scalar($dim) || is_bool($dim)) {
  240. return false;
  241. }
  242. return preg_match('/^(-|x)?\d+$/', (string) $dim);
  243. }
  244. /**
  245. * Determine whether to use default base URL, or base URL from options
  246. *
  247. * @param array $options
  248. * @return string
  249. */
  250. protected function _mergeBaseUrl(array $options)
  251. {
  252. if (null === $options['base_url']) {
  253. return $this->getBaseUrl();
  254. }
  255. return rtrim($options['base_url'], '/') . '/';
  256. }
  257. /**
  258. * Determine whether to use default format or format provided in options.
  259. *
  260. * @param array $options
  261. * @return string
  262. */
  263. protected function _mergeFormat(array $options)
  264. {
  265. if (in_array($options['format'], array('png', 'jpeg'))) {
  266. return '/' . $options['format'];
  267. }
  268. return $this->_format;
  269. }
  270. /**
  271. * Determine whether to use default dimensions, or those passed in options.
  272. *
  273. * @param array $options
  274. * @return string
  275. */
  276. protected function _mergeDimensions(array $options)
  277. {
  278. if (!$this->_validateDimension($options['width'])) {
  279. return $this->_dimensions;
  280. }
  281. $dimensions = '/' . $options['width'];
  282. if (!$this->_validateDimension($options['height'])) {
  283. return $dimensions;
  284. }
  285. $dimensions .= '/' . $options['height'];
  286. return $dimensions;
  287. }
  288. }