00001 <?php
00025 require_once 'Zend/Validate/Abstract.php';
00026
00035 class Zend_Validate_File_ImageSize extends Zend_Validate_Abstract
00036 {
00040 const WIDTH_TOO_BIG = 'fileImageSizeWidthTooBig';
00041 const WIDTH_TOO_SMALL = 'fileImageSizeWidthTooSmall';
00042 const HEIGHT_TOO_BIG = 'fileImageSizeHeightTooBig';
00043 const HEIGHT_TOO_SMALL = 'fileImageSizeHeightTooSmall';
00044 const NOT_DETECTED = 'fileImageSizeNotDetected';
00045 const NOT_READABLE = 'fileImageSizeNotReadable';
00046
00050 protected $_messageTemplates = array(
00051 self::WIDTH_TOO_BIG => "Maximum allowed width for image '%value%' should be '%maxwidth%' but '%width%' detected",
00052 self::WIDTH_TOO_SMALL => "Minimum expected width for image '%value%' should be '%minwidth%' but '%width%' detected",
00053 self::HEIGHT_TOO_BIG => "Maximum allowed height for image '%value%' should be '%maxheight%' but '%height%' detected",
00054 self::HEIGHT_TOO_SMALL => "Minimum expected height for image '%value%' should be '%minheight%' but '%height%' detected",
00055 self::NOT_DETECTED => "The size of image '%value%' could not be detected",
00056 self::NOT_READABLE => "The image '%value%' can not be read"
00057 );
00058
00062 protected $_messageVariables = array(
00063 'minwidth' => '_minwidth',
00064 'maxwidth' => '_maxwidth',
00065 'minheight' => '_minheight',
00066 'maxheight' => '_maxheight',
00067 'width' => '_width',
00068 'height' => '_height'
00069 );
00070
00076 protected $_minwidth;
00077
00083 protected $_maxwidth;
00084
00090 protected $_minheight;
00091
00097 protected $_maxheight;
00098
00104 protected $_width;
00105
00111 protected $_height;
00112
00125 public function __construct($options)
00126 {
00127 if ($options instanceof Zend_Config) {
00128 $options = $options->toArray();
00129 } elseif (1 < func_num_args()) {
00130
00131
00132 if (!is_array($options)) {
00133 $options = array('minwidth' => $options);
00134 }
00135 $argv = func_get_args();
00136 array_shift($argv);
00137 $options['minheight'] = array_shift($argv);
00138 if (!empty($argv)) {
00139 $options['maxwidth'] = array_shift($argv);
00140 if (!empty($argv)) {
00141 $options['maxheight'] = array_shift($argv);
00142 }
00143 }
00144 } else if (!is_array($options)) {
00145 require_once 'Zend/Validate/Exception.php';
00146 throw new Zend_Validate_Exception ('Invalid options to validator provided');
00147 }
00148
00149 if (isset($options['minheight']) || isset($options['minwidth'])) {
00150 $this->setImageMin($options);
00151 }
00152
00153 if (isset($options['maxheight']) || isset($options['maxwidth'])) {
00154 $this->setImageMax($options);
00155 }
00156 }
00157
00163 public function getImageMin()
00164 {
00165 return array('minwidth' => $this->_minwidth, 'minheight' => $this->_minheight);
00166 }
00167
00173 public function getImageMax()
00174 {
00175 return array('maxwidth' => $this->_maxwidth, 'maxheight' => $this->_maxheight);
00176 }
00177
00183 public function getImageWidth()
00184 {
00185 return array('minwidth' => $this->_minwidth, 'maxwidth' => $this->_maxwidth);
00186 }
00187
00193 public function getImageHeight()
00194 {
00195 return array('minheight' => $this->_minheight, 'maxheight' => $this->_maxheight);
00196 }
00197
00206 public function setImageMin($options)
00207 {
00208 if (isset($options['minwidth'])) {
00209 if (($this->_maxwidth !== null) and ($options['minwidth'] > $this->_maxwidth)) {
00210 require_once 'Zend/Validate/Exception.php';
00211 throw new Zend_Validate_Exception("The minimum image width must be less than or equal to the "
00212 . " maximum image width, but {$options['minwidth']} > {$this->_maxwidth}");
00213 }
00214 }
00215
00216 if (isset($options['maxheight'])) {
00217 if (($this->_maxheight !== null) and ($options['minheight'] > $this->_maxheight)) {
00218 require_once 'Zend/Validate/Exception.php';
00219 throw new Zend_Validate_Exception("The minimum image height must be less than or equal to the "
00220 . " maximum image height, but {$options['minheight']} > {$this->_maxheight}");
00221 }
00222 }
00223
00224 if (isset($options['minwidth'])) {
00225 $this->_minwidth = (int) $options['minwidth'];
00226 }
00227
00228 if (isset($options['minheight'])) {
00229 $this->_minheight = (int) $options['minheight'];
00230 }
00231
00232 return $this;
00233 }
00234
00243 public function setImageMax($options)
00244 {
00245 if (isset($options['maxwidth'])) {
00246 if (($this->_minwidth !== null) and ($options['maxwidth'] < $this->_minwidth)) {
00247 require_once 'Zend/Validate/Exception.php';
00248 throw new Zend_Validate_Exception("The maximum image width must be greater than or equal to the "
00249 . "minimum image width, but {$options['maxwidth']} < {$this->_minwidth}");
00250 }
00251 }
00252
00253 if (isset($options['maxheight'])) {
00254 if (($this->_minheight !== null) and ($options['maxheight'] < $this->_minheight)) {
00255 require_once 'Zend/Validate/Exception.php';
00256 throw new Zend_Validate_Exception("The maximum image height must be greater than or equal to the "
00257 . "minimum image height, but {$options['maxheight']} < {$this->_minwidth}");
00258 }
00259 }
00260
00261 if (isset($options['maxwidth'])) {
00262 $this->_maxwidth = (int) $options['maxwidth'];
00263 }
00264
00265 if (isset($options['maxheight'])) {
00266 $this->_maxheight = (int) $options['maxheight'];
00267 }
00268
00269 return $this;
00270 }
00271
00278 public function setImageWidth($options)
00279 {
00280 $this->setImageMin($options);
00281 $this->setImageMax($options);
00282
00283 return $this;
00284 }
00285
00292 public function setImageHeight($options)
00293 {
00294 $this->setImageMin($options);
00295 $this->setImageMax($options);
00296
00297 return $this;
00298 }
00299
00310 public function isValid($value, $file = null)
00311 {
00312
00313 require_once 'Zend/Loader.php';
00314 if (!Zend_Loader::isReadable($value)) {
00315 return $this->_throw($file, self::NOT_READABLE);
00316 }
00317
00318 $size = @getimagesize($value);
00319 $this->_setValue($file);
00320
00321 if (empty($size) or ($size[0] === 0) or ($size[1] === 0)) {
00322 return $this->_throw($file, self::NOT_DETECTED);
00323 }
00324
00325 $this->_width = $size[0];
00326 $this->_height = $size[1];
00327 if ($this->_width < $this->_minwidth) {
00328 $this->_throw($file, self::WIDTH_TOO_SMALL);
00329 }
00330
00331 if (($this->_maxwidth !== null) and ($this->_maxwidth < $this->_width)) {
00332 $this->_throw($file, self::WIDTH_TOO_BIG);
00333 }
00334
00335 if ($this->_height < $this->_minheight) {
00336 $this->_throw($file, self::HEIGHT_TOO_SMALL);
00337 }
00338
00339 if (($this->_maxheight !== null) and ($this->_maxheight < $this->_height)) {
00340 $this->_throw($file, self::HEIGHT_TOO_BIG);
00341 }
00342
00343 if (count($this->_messages) > 0) {
00344 return false;
00345 }
00346
00347 return true;
00348 }
00349
00357 protected function _throw($file, $errorType)
00358 {
00359 if ($file !== null) {
00360 $this->_value = $file['name'];
00361 }
00362
00363 $this->_error($errorType);
00364 return false;
00365 }
00366 }