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