3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Validator\File
;
12 use Zend\Stdlib\ErrorHandler
;
13 use Zend\Validator\AbstractValidator
;
14 use Zend\Validator\Exception
;
17 * Validator for the image size of a image file
19 class ImageSize
extends AbstractValidator
22 * @const string Error constants
24 const WIDTH_TOO_BIG
= 'fileImageSizeWidthTooBig';
25 const WIDTH_TOO_SMALL
= 'fileImageSizeWidthTooSmall';
26 const HEIGHT_TOO_BIG
= 'fileImageSizeHeightTooBig';
27 const HEIGHT_TOO_SMALL
= 'fileImageSizeHeightTooSmall';
28 const NOT_DETECTED
= 'fileImageSizeNotDetected';
29 const NOT_READABLE
= 'fileImageSizeNotReadable';
32 * @var array Error message template
34 protected $messageTemplates = array(
35 self
::WIDTH_TOO_BIG
=> "Maximum allowed width for image should be '%maxwidth%' but '%width%' detected",
36 self
::WIDTH_TOO_SMALL
=> "Minimum expected width for image should be '%minwidth%' but '%width%' detected",
37 self
::HEIGHT_TOO_BIG
=> "Maximum allowed height for image should be '%maxheight%' but '%height%' detected",
38 self
::HEIGHT_TOO_SMALL
=> "Minimum expected height for image should be '%minheight%' but '%height%' detected",
39 self
::NOT_DETECTED
=> "The size of image could not be detected",
40 self
::NOT_READABLE
=> "File is not readable or does not exist",
44 * @var array Error message template variables
46 protected $messageVariables = array(
47 'minwidth' => array('options' => 'minWidth'),
48 'maxwidth' => array('options' => 'maxWidth'),
49 'minheight' => array('options' => 'minHeight'),
50 'maxheight' => array('options' => 'maxHeight'),
70 * Options for this validator
74 protected $options = array(
75 'minWidth' => null, // Minimum image width
76 'maxWidth' => null, // Maximum image width
77 'minHeight' => null, // Minimum image height
78 'maxHeight' => null, // Maximum image height
82 * Sets validator options
84 * Accepts the following option keys:
90 * @param array|\Traversable $options
92 public function __construct($options = null)
94 if (1 < func_num_args()) {
95 if (!is_array($options)) {
96 $options = array('minWidth' => $options);
99 $argv = func_get_args();
101 $options['minHeight'] = array_shift($argv);
103 $options['maxWidth'] = array_shift($argv);
105 $options['maxHeight'] = array_shift($argv);
110 parent
::__construct($options);
114 * Returns the minimum allowed width
118 public function getMinWidth()
120 return $this->options
['minWidth'];
124 * Sets the minimum allowed width
126 * @param int $minWidth
127 * @return ImageSize Provides a fluid interface
128 * @throws Exception\InvalidArgumentException When minwidth is greater than maxwidth
130 public function setMinWidth($minWidth)
132 if (($this->getMaxWidth() !== null) && ($minWidth > $this->getMaxWidth())) {
133 throw new Exception\
InvalidArgumentException("The minimum image width must be less than or equal to the "
134 . " maximum image width, but {$minWidth} > {$this->getMaxWidth()}");
137 $this->options
['minWidth'] = (int) $minWidth;
142 * Returns the maximum allowed width
146 public function getMaxWidth()
148 return $this->options
['maxWidth'];
152 * Sets the maximum allowed width
154 * @param int $maxWidth
155 * @return ImageSize Provides a fluid interface
156 * @throws Exception\InvalidArgumentException When maxwidth is less than minwidth
158 public function setMaxWidth($maxWidth)
160 if (($this->getMinWidth() !== null) && ($maxWidth < $this->getMinWidth())) {
161 throw new Exception\
InvalidArgumentException("The maximum image width must be greater than or equal to the "
162 . "minimum image width, but {$maxWidth} < {$this->getMinWidth()}");
165 $this->options
['maxWidth'] = (int) $maxWidth;
170 * Returns the minimum allowed height
174 public function getMinHeight()
176 return $this->options
['minHeight'];
180 * Sets the minimum allowed height
182 * @param int $minHeight
183 * @return ImageSize Provides a fluid interface
184 * @throws Exception\InvalidArgumentException When minheight is greater than maxheight
186 public function setMinHeight($minHeight)
188 if (($this->getMaxHeight() !== null) && ($minHeight > $this->getMaxHeight())) {
189 throw new Exception\
InvalidArgumentException("The minimum image height must be less than or equal to the "
190 . " maximum image height, but {$minHeight} > {$this->getMaxHeight()}");
193 $this->options
['minHeight'] = (int) $minHeight;
198 * Returns the maximum allowed height
202 public function getMaxHeight()
204 return $this->options
['maxHeight'];
208 * Sets the maximum allowed height
210 * @param int $maxHeight
211 * @return ImageSize Provides a fluid interface
212 * @throws Exception\InvalidArgumentException When maxheight is less than minheight
214 public function setMaxHeight($maxHeight)
216 if (($this->getMinHeight() !== null) && ($maxHeight < $this->getMinHeight())) {
217 throw new Exception\
InvalidArgumentException("The maximum image height must be greater than or equal to the "
218 . "minimum image height, but {$maxHeight} < {$this->getMinHeight()}");
221 $this->options
['maxHeight'] = (int) $maxHeight;
226 * Returns the set minimum image sizes
230 public function getImageMin()
232 return array('minWidth' => $this->getMinWidth(), 'minHeight' => $this->getMinHeight());
236 * Returns the set maximum image sizes
240 public function getImageMax()
242 return array('maxWidth' => $this->getMaxWidth(), 'maxHeight' => $this->getMaxHeight());
246 * Returns the set image width sizes
250 public function getImageWidth()
252 return array('minWidth' => $this->getMinWidth(), 'maxWidth' => $this->getMaxWidth());
256 * Returns the set image height sizes
260 public function getImageHeight()
262 return array('minHeight' => $this->getMinHeight(), 'maxHeight' => $this->getMaxHeight());
266 * Sets the minimum image size
268 * @param array $options The minimum image dimensions
269 * @return ImageSize Provides a fluent interface
271 public function setImageMin($options)
273 $this->setOptions($options);
278 * Sets the maximum image size
280 * @param array|\Traversable $options The maximum image dimensions
281 * @return ImageSize Provides a fluent interface
283 public function setImageMax($options)
285 $this->setOptions($options);
290 * Sets the minimum and maximum image width
292 * @param array $options The image width dimensions
293 * @return ImageSize Provides a fluent interface
295 public function setImageWidth($options)
297 $this->setImageMin($options);
298 $this->setImageMax($options);
304 * Sets the minimum and maximum image height
306 * @param array $options The image height dimensions
307 * @return ImageSize Provides a fluent interface
309 public function setImageHeight($options)
311 $this->setImageMin($options);
312 $this->setImageMax($options);
318 * Returns true if and only if the image size of $value is at least min and
319 * not bigger than max
321 * @param string|array $value Real file to check for image size
322 * @param array $file File data from \Zend\File\Transfer\Transfer (optional)
325 public function isValid($value, $file = null)
327 if (is_string($value) && is_array($file)) {
328 // Legacy Zend\Transfer API support
329 $filename = $file['name'];
330 $file = $file['tmp_name'];
331 } elseif (is_array($value)) {
332 if (!isset($value['tmp_name']) ||
!isset($value['name'])) {
333 throw new Exception\
InvalidArgumentException(
334 'Value array must be in $_FILES format'
337 $file = $value['tmp_name'];
338 $filename = $value['name'];
341 $filename = basename($file);
343 $this->setValue($filename);
345 // Is file readable ?
346 if (false === stream_resolve_include_path($file)) {
347 $this->error(self
::NOT_READABLE
);
351 ErrorHandler
::start();
352 $size = getimagesize($file);
353 ErrorHandler
::stop();
355 if (empty($size) ||
($size[0] === 0) ||
($size[1] === 0)) {
356 $this->error(self
::NOT_DETECTED
);
360 $this->width
= $size[0];
361 $this->height
= $size[1];
362 if ($this->width
< $this->getMinWidth()) {
363 $this->error(self
::WIDTH_TOO_SMALL
);
366 if (($this->getMaxWidth() !== null) && ($this->getMaxWidth() < $this->width
)) {
367 $this->error(self
::WIDTH_TOO_BIG
);
370 if ($this->height
< $this->getMinHeight()) {
371 $this->error(self
::HEIGHT_TOO_SMALL
);
374 if (($this->getMaxHeight() !== null) && ($this->getMaxHeight() < $this->height
)) {
375 $this->error(self
::HEIGHT_TOO_BIG
);
378 if (count($this->getMessages()) > 0) {