|
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_Validate |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: ImageSize.php 22668 2010-07-25 14:50:46Z thomas $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @see Zend_Validate_Abstract |
|
24 */ |
|
25 require_once 'Zend/Validate/Abstract.php'; |
|
26 |
|
27 /** |
|
28 * Validator for the image size of a image file |
|
29 * |
|
30 * @category Zend |
|
31 * @package Zend_Validate |
|
32 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
33 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
34 */ |
|
35 class Zend_Validate_File_ImageSize extends Zend_Validate_Abstract |
|
36 { |
|
37 /** |
|
38 * @const string Error constants |
|
39 */ |
|
40 const WIDTH_TOO_BIG = 'fileImageSizeWidthTooBig'; |
|
41 const WIDTH_TOO_SMALL = 'fileImageSizeWidthTooSmall'; |
|
42 const HEIGHT_TOO_BIG = 'fileImageSizeHeightTooBig'; |
|
43 const HEIGHT_TOO_SMALL = 'fileImageSizeHeightTooSmall'; |
|
44 const NOT_DETECTED = 'fileImageSizeNotDetected'; |
|
45 const NOT_READABLE = 'fileImageSizeNotReadable'; |
|
46 |
|
47 /** |
|
48 * @var array Error message template |
|
49 */ |
|
50 protected $_messageTemplates = array( |
|
51 self::WIDTH_TOO_BIG => "Maximum allowed width for image '%value%' should be '%maxwidth%' but '%width%' detected", |
|
52 self::WIDTH_TOO_SMALL => "Minimum expected width for image '%value%' should be '%minwidth%' but '%width%' detected", |
|
53 self::HEIGHT_TOO_BIG => "Maximum allowed height for image '%value%' should be '%maxheight%' but '%height%' detected", |
|
54 self::HEIGHT_TOO_SMALL => "Minimum expected height for image '%value%' should be '%minheight%' but '%height%' detected", |
|
55 self::NOT_DETECTED => "The size of image '%value%' could not be detected", |
|
56 self::NOT_READABLE => "File '%value%' is not readable or does not exist", |
|
57 ); |
|
58 |
|
59 /** |
|
60 * @var array Error message template variables |
|
61 */ |
|
62 protected $_messageVariables = array( |
|
63 'minwidth' => '_minwidth', |
|
64 'maxwidth' => '_maxwidth', |
|
65 'minheight' => '_minheight', |
|
66 'maxheight' => '_maxheight', |
|
67 'width' => '_width', |
|
68 'height' => '_height' |
|
69 ); |
|
70 |
|
71 /** |
|
72 * Minimum image width |
|
73 * |
|
74 * @var integer |
|
75 */ |
|
76 protected $_minwidth; |
|
77 |
|
78 /** |
|
79 * Maximum image width |
|
80 * |
|
81 * @var integer |
|
82 */ |
|
83 protected $_maxwidth; |
|
84 |
|
85 /** |
|
86 * Minimum image height |
|
87 * |
|
88 * @var integer |
|
89 */ |
|
90 protected $_minheight; |
|
91 |
|
92 /** |
|
93 * Maximum image height |
|
94 * |
|
95 * @var integer |
|
96 */ |
|
97 protected $_maxheight; |
|
98 |
|
99 /** |
|
100 * Detected width |
|
101 * |
|
102 * @var integer |
|
103 */ |
|
104 protected $_width; |
|
105 |
|
106 /** |
|
107 * Detected height |
|
108 * |
|
109 * @var integer |
|
110 */ |
|
111 protected $_height; |
|
112 |
|
113 /** |
|
114 * Sets validator options |
|
115 * |
|
116 * Accepts the following option keys: |
|
117 * - minheight |
|
118 * - minwidth |
|
119 * - maxheight |
|
120 * - maxwidth |
|
121 * |
|
122 * @param Zend_Config|array $options |
|
123 * @return void |
|
124 */ |
|
125 public function __construct($options) |
|
126 { |
|
127 if ($options instanceof Zend_Config) { |
|
128 $options = $options->toArray(); |
|
129 } elseif (1 < func_num_args()) { |
|
130 if (!is_array($options)) { |
|
131 $options = array('minwidth' => $options); |
|
132 } |
|
133 $argv = func_get_args(); |
|
134 array_shift($argv); |
|
135 $options['minheight'] = array_shift($argv); |
|
136 if (!empty($argv)) { |
|
137 $options['maxwidth'] = array_shift($argv); |
|
138 if (!empty($argv)) { |
|
139 $options['maxheight'] = array_shift($argv); |
|
140 } |
|
141 } |
|
142 } else if (!is_array($options)) { |
|
143 require_once 'Zend/Validate/Exception.php'; |
|
144 throw new Zend_Validate_Exception ('Invalid options to validator provided'); |
|
145 } |
|
146 |
|
147 if (isset($options['minheight']) || isset($options['minwidth'])) { |
|
148 $this->setImageMin($options); |
|
149 } |
|
150 |
|
151 if (isset($options['maxheight']) || isset($options['maxwidth'])) { |
|
152 $this->setImageMax($options); |
|
153 } |
|
154 } |
|
155 |
|
156 /** |
|
157 * Returns the set minimum image sizes |
|
158 * |
|
159 * @return array |
|
160 */ |
|
161 public function getImageMin() |
|
162 { |
|
163 return array('minwidth' => $this->_minwidth, 'minheight' => $this->_minheight); |
|
164 } |
|
165 |
|
166 /** |
|
167 * Returns the set maximum image sizes |
|
168 * |
|
169 * @return array |
|
170 */ |
|
171 public function getImageMax() |
|
172 { |
|
173 return array('maxwidth' => $this->_maxwidth, 'maxheight' => $this->_maxheight); |
|
174 } |
|
175 |
|
176 /** |
|
177 * Returns the set image width sizes |
|
178 * |
|
179 * @return array |
|
180 */ |
|
181 public function getImageWidth() |
|
182 { |
|
183 return array('minwidth' => $this->_minwidth, 'maxwidth' => $this->_maxwidth); |
|
184 } |
|
185 |
|
186 /** |
|
187 * Returns the set image height sizes |
|
188 * |
|
189 * @return array |
|
190 */ |
|
191 public function getImageHeight() |
|
192 { |
|
193 return array('minheight' => $this->_minheight, 'maxheight' => $this->_maxheight); |
|
194 } |
|
195 |
|
196 /** |
|
197 * Sets the minimum image size |
|
198 * |
|
199 * @param array $options The minimum image dimensions |
|
200 * @throws Zend_Validate_Exception When minwidth is greater than maxwidth |
|
201 * @throws Zend_Validate_Exception When minheight is greater than maxheight |
|
202 * @return Zend_Validate_File_ImageSize Provides a fluent interface |
|
203 */ |
|
204 public function setImageMin($options) |
|
205 { |
|
206 if (isset($options['minwidth'])) { |
|
207 if (($this->_maxwidth !== null) and ($options['minwidth'] > $this->_maxwidth)) { |
|
208 require_once 'Zend/Validate/Exception.php'; |
|
209 throw new Zend_Validate_Exception("The minimum image width must be less than or equal to the " |
|
210 . " maximum image width, but {$options['minwidth']} > {$this->_maxwidth}"); |
|
211 } |
|
212 } |
|
213 |
|
214 if (isset($options['maxheight'])) { |
|
215 if (($this->_maxheight !== null) and ($options['minheight'] > $this->_maxheight)) { |
|
216 require_once 'Zend/Validate/Exception.php'; |
|
217 throw new Zend_Validate_Exception("The minimum image height must be less than or equal to the " |
|
218 . " maximum image height, but {$options['minheight']} > {$this->_maxheight}"); |
|
219 } |
|
220 } |
|
221 |
|
222 if (isset($options['minwidth'])) { |
|
223 $this->_minwidth = (int) $options['minwidth']; |
|
224 } |
|
225 |
|
226 if (isset($options['minheight'])) { |
|
227 $this->_minheight = (int) $options['minheight']; |
|
228 } |
|
229 |
|
230 return $this; |
|
231 } |
|
232 |
|
233 /** |
|
234 * Sets the maximum image size |
|
235 * |
|
236 * @param array $options The maximum image dimensions |
|
237 * @throws Zend_Validate_Exception When maxwidth is smaller than minwidth |
|
238 * @throws Zend_Validate_Exception When maxheight is smaller than minheight |
|
239 * @return Zend_Validate_StringLength Provides a fluent interface |
|
240 */ |
|
241 public function setImageMax($options) |
|
242 { |
|
243 if (isset($options['maxwidth'])) { |
|
244 if (($this->_minwidth !== null) and ($options['maxwidth'] < $this->_minwidth)) { |
|
245 require_once 'Zend/Validate/Exception.php'; |
|
246 throw new Zend_Validate_Exception("The maximum image width must be greater than or equal to the " |
|
247 . "minimum image width, but {$options['maxwidth']} < {$this->_minwidth}"); |
|
248 } |
|
249 } |
|
250 |
|
251 if (isset($options['maxheight'])) { |
|
252 if (($this->_minheight !== null) and ($options['maxheight'] < $this->_minheight)) { |
|
253 require_once 'Zend/Validate/Exception.php'; |
|
254 throw new Zend_Validate_Exception("The maximum image height must be greater than or equal to the " |
|
255 . "minimum image height, but {$options['maxheight']} < {$this->_minwidth}"); |
|
256 } |
|
257 } |
|
258 |
|
259 if (isset($options['maxwidth'])) { |
|
260 $this->_maxwidth = (int) $options['maxwidth']; |
|
261 } |
|
262 |
|
263 if (isset($options['maxheight'])) { |
|
264 $this->_maxheight = (int) $options['maxheight']; |
|
265 } |
|
266 |
|
267 return $this; |
|
268 } |
|
269 |
|
270 /** |
|
271 * Sets the mimimum and maximum image width |
|
272 * |
|
273 * @param array $options The image width dimensions |
|
274 * @return Zend_Validate_File_ImageSize Provides a fluent interface |
|
275 */ |
|
276 public function setImageWidth($options) |
|
277 { |
|
278 $this->setImageMin($options); |
|
279 $this->setImageMax($options); |
|
280 |
|
281 return $this; |
|
282 } |
|
283 |
|
284 /** |
|
285 * Sets the mimimum and maximum image height |
|
286 * |
|
287 * @param array $options The image height dimensions |
|
288 * @return Zend_Validate_File_ImageSize Provides a fluent interface |
|
289 */ |
|
290 public function setImageHeight($options) |
|
291 { |
|
292 $this->setImageMin($options); |
|
293 $this->setImageMax($options); |
|
294 |
|
295 return $this; |
|
296 } |
|
297 |
|
298 /** |
|
299 * Defined by Zend_Validate_Interface |
|
300 * |
|
301 * Returns true if and only if the imagesize of $value is at least min and |
|
302 * not bigger than max |
|
303 * |
|
304 * @param string $value Real file to check for image size |
|
305 * @param array $file File data from Zend_File_Transfer |
|
306 * @return boolean |
|
307 */ |
|
308 public function isValid($value, $file = null) |
|
309 { |
|
310 // Is file readable ? |
|
311 require_once 'Zend/Loader.php'; |
|
312 if (!Zend_Loader::isReadable($value)) { |
|
313 return $this->_throw($file, self::NOT_READABLE); |
|
314 } |
|
315 |
|
316 $size = @getimagesize($value); |
|
317 $this->_setValue($file); |
|
318 |
|
319 if (empty($size) or ($size[0] === 0) or ($size[1] === 0)) { |
|
320 return $this->_throw($file, self::NOT_DETECTED); |
|
321 } |
|
322 |
|
323 $this->_width = $size[0]; |
|
324 $this->_height = $size[1]; |
|
325 if ($this->_width < $this->_minwidth) { |
|
326 $this->_throw($file, self::WIDTH_TOO_SMALL); |
|
327 } |
|
328 |
|
329 if (($this->_maxwidth !== null) and ($this->_maxwidth < $this->_width)) { |
|
330 $this->_throw($file, self::WIDTH_TOO_BIG); |
|
331 } |
|
332 |
|
333 if ($this->_height < $this->_minheight) { |
|
334 $this->_throw($file, self::HEIGHT_TOO_SMALL); |
|
335 } |
|
336 |
|
337 if (($this->_maxheight !== null) and ($this->_maxheight < $this->_height)) { |
|
338 $this->_throw($file, self::HEIGHT_TOO_BIG); |
|
339 } |
|
340 |
|
341 if (count($this->_messages) > 0) { |
|
342 return false; |
|
343 } |
|
344 |
|
345 return true; |
|
346 } |
|
347 |
|
348 /** |
|
349 * Throws an error of the given type |
|
350 * |
|
351 * @param string $file |
|
352 * @param string $errorType |
|
353 * @return false |
|
354 */ |
|
355 protected function _throw($file, $errorType) |
|
356 { |
|
357 if ($file !== null) { |
|
358 $this->_value = $file['name']; |
|
359 } |
|
360 |
|
361 $this->_error($errorType); |
|
362 return false; |
|
363 } |
|
364 } |