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