13 * to license@zend.com so we can send you a copy immediately. |
13 * to license@zend.com so we can send you a copy immediately. |
14 * |
14 * |
15 * @category Zend |
15 * @category Zend |
16 * @package Zend_View |
16 * @package Zend_View |
17 * @subpackage Helper |
17 * @subpackage Helper |
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
18 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
20 */ |
20 */ |
21 |
21 |
22 /** Zend_View_Helper_HtmlElement */ |
22 /** Zend_View_Helper_HtmlElement */ |
23 require_once 'Zend/View/Helper/HtmlElement.php'; |
23 require_once 'Zend/View/Helper/HtmlElement.php'; |
24 |
24 |
25 /** |
25 /** |
26 * Helper for generating urls and/or image tags for use with tinysrc.net |
26 * Helper for generating urls and/or image tags for use with tinysrc.net |
27 * |
27 * |
28 * tinysrc.net provides an API for generating scaled, browser device-specific |
28 * tinysrc.net provides an API for generating scaled, browser device-specific |
29 * images. In essence, you pass the API the URL to an image on your own server, |
29 * images. In essence, you pass the API the URL to an image on your own server, |
30 * and tinysrc.net then provides the appropriate image based on the device that |
30 * and tinysrc.net then provides the appropriate image based on the device that |
31 * accesses it. |
31 * accesses it. |
32 * |
32 * |
33 * Additionally, tinysrc.net allows you to specify additional configuration via |
33 * Additionally, tinysrc.net allows you to specify additional configuration via |
34 * the API: |
34 * the API: |
35 * |
35 * |
36 * - image size. You may define this as: |
36 * - image size. You may define this as: |
37 * - explicit size |
37 * - explicit size |
38 * - subtractive size (size of screen minus specified number of pixels) |
38 * - subtractive size (size of screen minus specified number of pixels) |
39 * - percentage size (percentage of screen size)) |
39 * - percentage size (percentage of screen size)) |
40 * - image format. This will convert the image to the given format; allowed |
40 * - image format. This will convert the image to the given format; allowed |
41 * values are "png" or "jpeg". By default, gif images are converted to png. |
41 * values are "png" or "jpeg". By default, gif images are converted to png. |
42 * |
42 * |
43 * This helper allows you to specify all configuration options, as well as: |
43 * This helper allows you to specify all configuration options, as well as: |
44 * |
44 * |
45 * - whether or not to generate the full image tag (or just the URL) |
45 * - whether or not to generate the full image tag (or just the URL) |
46 * - base url to images (which should include the protocol, server, and |
46 * - base url to images (which should include the protocol, server, and |
47 * optionally port and base path) |
47 * optionally port and base path) |
48 * |
48 * |
49 * @see http://tinysrc.net/ |
49 * @see http://tinysrc.net/ |
50 * @package Zend_View |
50 * @package Zend_View |
51 * @subpackage Helper |
51 * @subpackage Helper |
52 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
52 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
53 * @license http://framework.zend.com/license/new-bsd New BSD License |
53 * @license http://framework.zend.com/license/new-bsd New BSD License |
54 */ |
54 */ |
55 class Zend_View_Helper_TinySrc extends Zend_View_Helper_HtmlElement |
55 class Zend_View_Helper_TinySrc extends Zend_View_Helper_HtmlElement |
56 { |
56 { |
57 const TINYSRC_BASE = 'http://i.tinysrc.mobi'; |
57 const TINYSRC_BASE = 'http://i.tinysrc.mobi'; |
109 $defaultOptions['create_tag'] = $this->createTag(); |
109 $defaultOptions['create_tag'] = $this->createTag(); |
110 $options = array_merge($defaultOptions, $options); |
110 $options = array_merge($defaultOptions, $options); |
111 |
111 |
112 $url = '/' . $this->_mergeBaseUrl($options) . ltrim($image, '/'); |
112 $url = '/' . $this->_mergeBaseUrl($options) . ltrim($image, '/'); |
113 |
113 |
114 $src = self::TINYSRC_BASE |
114 $src = self::TINYSRC_BASE |
115 . $this->_mergeFormat($options) |
115 . $this->_mergeFormat($options) |
116 . $this->_mergeDimensions($options) |
116 . $this->_mergeDimensions($options) |
117 . $url; |
117 . $url; |
118 |
118 |
119 if (!$options['create_tag']) { |
119 if (!$options['create_tag']) { |
120 return $src; |
120 return $src; |
194 } |
194 } |
195 |
195 |
196 /** |
196 /** |
197 * Set default dimensions |
197 * Set default dimensions |
198 * |
198 * |
199 * If null is specified for width, default dimensions will be cleared. If |
199 * If null is specified for width, default dimensions will be cleared. If |
200 * only width is specified, only width will be used. If either dimension |
200 * only width is specified, only width will be used. If either dimension |
201 * fails validation, an exception is raised. |
201 * fails validation, an exception is raised. |
202 * |
202 * |
203 * @param null|int|string $width |
203 * @param null|int|string $width |
204 * @param null|int|string $height |
204 * @param null|int|string $height |
205 * @return Zend_View_Helper_TinySrc |
205 * @return Zend_View_Helper_TinySrc |
206 * @throws Zend_View_Exception |
206 * @throws Zend_View_Exception |
207 */ |
207 */ |
208 public function setDefaultDimensions($width = null, $height = null) |
208 public function setDefaultDimensions($width = null, $height = null) |
209 { |
209 { |
230 return $this; |
230 return $this; |
231 } |
231 } |
232 |
232 |
233 /** |
233 /** |
234 * Set state of "create tag" flag |
234 * Set state of "create tag" flag |
235 * |
235 * |
236 * @param bool $flag |
236 * @param bool $flag |
237 * @return Zend_View_Helper_TinySrc |
237 * @return Zend_View_Helper_TinySrc |
238 */ |
238 */ |
239 public function setCreateTag($flag) |
239 public function setCreateTag($flag) |
240 { |
240 { |
241 $this->_createTagFlag = (bool) $flag; |
241 $this->_createTagFlag = (bool) $flag; |
242 return $this; |
242 return $this; |
243 } |
243 } |
244 |
244 |
245 /** |
245 /** |
246 * Should the helper create an image tag? |
246 * Should the helper create an image tag? |
247 * |
247 * |
248 * @return bool |
248 * @return bool |
249 */ |
249 */ |
250 public function createTag() |
250 public function createTag() |
251 { |
251 { |
252 return $this->_createTagFlag; |
252 return $this->_createTagFlag; |
282 return rtrim($options['base_url'], '/') . '/'; |
282 return rtrim($options['base_url'], '/') . '/'; |
283 } |
283 } |
284 |
284 |
285 /** |
285 /** |
286 * Determine whether to use default format or format provided in options. |
286 * Determine whether to use default format or format provided in options. |
287 * |
287 * |
288 * @param array $options |
288 * @param array $options |
289 * @return string |
289 * @return string |
290 */ |
290 */ |
291 protected function _mergeFormat(array $options) |
291 protected function _mergeFormat(array $options) |
292 { |
292 { |
293 if (in_array($options['format'], array('png', 'jpeg'))) { |
293 if (in_array($options['format'], array('png', 'jpeg'))) { |
294 return '/' . $options['format']; |
294 return '/' . $options['format']; |
295 } |
295 } |
296 return $this->_format; |
296 return $this->_format; |
297 } |
297 } |
298 |
298 |
299 /** |
299 /** |
300 * Determine whether to use default dimensions, or those passed in options. |
300 * Determine whether to use default dimensions, or those passed in options. |
301 * |
301 * |
302 * @param array $options |
302 * @param array $options |
303 * @return string |
303 * @return string |
304 */ |
304 */ |
305 protected function _mergeDimensions(array $options) |
305 protected function _mergeDimensions(array $options) |
306 { |
306 { |
307 if (!$this->_validateDimension($options['width'])) { |
307 if (!$this->_validateDimension($options['width'])) { |