File indexing completed on 2024-12-22 05:37:11
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Validate 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * @see Zend_Validate_Abstract 0024 */ 0025 // require_once 'Zend/Validate/Abstract.php'; 0026 0027 /** 0028 * Validator for the image size of a image file 0029 * 0030 * @category Zend 0031 * @package Zend_Validate 0032 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0033 * @license http://framework.zend.com/license/new-bsd New BSD License 0034 */ 0035 class Zend_Validate_File_ImageSize extends Zend_Validate_Abstract 0036 { 0037 /** 0038 * @const string Error constants 0039 */ 0040 const WIDTH_TOO_BIG = 'fileImageSizeWidthTooBig'; 0041 const WIDTH_TOO_SMALL = 'fileImageSizeWidthTooSmall'; 0042 const HEIGHT_TOO_BIG = 'fileImageSizeHeightTooBig'; 0043 const HEIGHT_TOO_SMALL = 'fileImageSizeHeightTooSmall'; 0044 const NOT_DETECTED = 'fileImageSizeNotDetected'; 0045 const NOT_READABLE = 'fileImageSizeNotReadable'; 0046 0047 /** 0048 * @var array Error message template 0049 */ 0050 protected $_messageTemplates = array( 0051 self::WIDTH_TOO_BIG => "Maximum allowed width for image '%value%' should be '%maxwidth%' but '%width%' detected", 0052 self::WIDTH_TOO_SMALL => "Minimum expected width for image '%value%' should be '%minwidth%' but '%width%' detected", 0053 self::HEIGHT_TOO_BIG => "Maximum allowed height for image '%value%' should be '%maxheight%' but '%height%' detected", 0054 self::HEIGHT_TOO_SMALL => "Minimum expected height for image '%value%' should be '%minheight%' but '%height%' detected", 0055 self::NOT_DETECTED => "The size of image '%value%' could not be detected", 0056 self::NOT_READABLE => "File '%value%' is not readable or does not exist", 0057 ); 0058 0059 /** 0060 * @var array Error message template variables 0061 */ 0062 protected $_messageVariables = array( 0063 'minwidth' => '_minwidth', 0064 'maxwidth' => '_maxwidth', 0065 'minheight' => '_minheight', 0066 'maxheight' => '_maxheight', 0067 'width' => '_width', 0068 'height' => '_height' 0069 ); 0070 0071 /** 0072 * Minimum image width 0073 * 0074 * @var integer 0075 */ 0076 protected $_minwidth; 0077 0078 /** 0079 * Maximum image width 0080 * 0081 * @var integer 0082 */ 0083 protected $_maxwidth; 0084 0085 /** 0086 * Minimum image height 0087 * 0088 * @var integer 0089 */ 0090 protected $_minheight; 0091 0092 /** 0093 * Maximum image height 0094 * 0095 * @var integer 0096 */ 0097 protected $_maxheight; 0098 0099 /** 0100 * Detected width 0101 * 0102 * @var integer 0103 */ 0104 protected $_width; 0105 0106 /** 0107 * Detected height 0108 * 0109 * @var integer 0110 */ 0111 protected $_height; 0112 0113 /** 0114 * Sets validator options 0115 * 0116 * Accepts the following option keys: 0117 * - minheight 0118 * - minwidth 0119 * - maxheight 0120 * - maxwidth 0121 * 0122 * @param Zend_Config|array $options 0123 * @throws Zend_Validate_Exception 0124 */ 0125 public function __construct($options) 0126 { 0127 if ($options instanceof Zend_Config) { 0128 $options = $options->toArray(); 0129 } elseif (1 < func_num_args()) { 0130 if (!is_array($options)) { 0131 $options = array('minwidth' => $options); 0132 } 0133 $argv = func_get_args(); 0134 array_shift($argv); 0135 $options['minheight'] = array_shift($argv); 0136 if (!empty($argv)) { 0137 $options['maxwidth'] = array_shift($argv); 0138 if (!empty($argv)) { 0139 $options['maxheight'] = array_shift($argv); 0140 } 0141 } 0142 } else if (!is_array($options)) { 0143 // require_once 'Zend/Validate/Exception.php'; 0144 throw new Zend_Validate_Exception ('Invalid options to validator provided'); 0145 } 0146 0147 if (isset($options['minheight']) || isset($options['minwidth'])) { 0148 $this->setImageMin($options); 0149 } 0150 0151 if (isset($options['maxheight']) || isset($options['maxwidth'])) { 0152 $this->setImageMax($options); 0153 } 0154 } 0155 0156 /** 0157 * Returns the set minimum image sizes 0158 * 0159 * @return array 0160 */ 0161 public function getImageMin() 0162 { 0163 return array('minwidth' => $this->_minwidth, 'minheight' => $this->_minheight); 0164 } 0165 0166 /** 0167 * Returns the set maximum image sizes 0168 * 0169 * @return array 0170 */ 0171 public function getImageMax() 0172 { 0173 return array('maxwidth' => $this->_maxwidth, 'maxheight' => $this->_maxheight); 0174 } 0175 0176 /** 0177 * Returns the set image width sizes 0178 * 0179 * @return array 0180 */ 0181 public function getImageWidth() 0182 { 0183 return array('minwidth' => $this->_minwidth, 'maxwidth' => $this->_maxwidth); 0184 } 0185 0186 /** 0187 * Returns the set image height sizes 0188 * 0189 * @return array 0190 */ 0191 public function getImageHeight() 0192 { 0193 return array('minheight' => $this->_minheight, 'maxheight' => $this->_maxheight); 0194 } 0195 0196 /** 0197 * Sets the minimum image size 0198 * 0199 * @param array $options The minimum image dimensions 0200 * @throws Zend_Validate_Exception When minwidth is greater than maxwidth 0201 * @throws Zend_Validate_Exception When minheight is greater than maxheight 0202 * @return Zend_Validate_File_ImageSize Provides a fluent interface 0203 */ 0204 public function setImageMin($options) 0205 { 0206 if (isset($options['minwidth'])) { 0207 if (($this->_maxwidth !== null) and ($options['minwidth'] > $this->_maxwidth)) { 0208 // require_once 'Zend/Validate/Exception.php'; 0209 throw new Zend_Validate_Exception("The minimum image width must be less than or equal to the " 0210 . " maximum image width, but {$options['minwidth']} > {$this->_maxwidth}"); 0211 } 0212 } 0213 0214 if (isset($options['maxheight'])) { 0215 if (($this->_maxheight !== null) and ($options['minheight'] > $this->_maxheight)) { 0216 // require_once 'Zend/Validate/Exception.php'; 0217 throw new Zend_Validate_Exception("The minimum image height must be less than or equal to the " 0218 . " maximum image height, but {$options['minheight']} > {$this->_maxheight}"); 0219 } 0220 } 0221 0222 if (isset($options['minwidth'])) { 0223 $this->_minwidth = (int) $options['minwidth']; 0224 } 0225 0226 if (isset($options['minheight'])) { 0227 $this->_minheight = (int) $options['minheight']; 0228 } 0229 0230 return $this; 0231 } 0232 0233 /** 0234 * Sets the maximum image size 0235 * 0236 * @param array $options The maximum image dimensions 0237 * @throws Zend_Validate_Exception When maxwidth is smaller than minwidth 0238 * @throws Zend_Validate_Exception When maxheight is smaller than minheight 0239 * @return Zend_Validate_StringLength Provides a fluent interface 0240 */ 0241 public function setImageMax($options) 0242 { 0243 if (isset($options['maxwidth'])) { 0244 if (($this->_minwidth !== null) and ($options['maxwidth'] < $this->_minwidth)) { 0245 // require_once 'Zend/Validate/Exception.php'; 0246 throw new Zend_Validate_Exception("The maximum image width must be greater than or equal to the " 0247 . "minimum image width, but {$options['maxwidth']} < {$this->_minwidth}"); 0248 } 0249 } 0250 0251 if (isset($options['maxheight'])) { 0252 if (($this->_minheight !== null) and ($options['maxheight'] < $this->_minheight)) { 0253 // require_once 'Zend/Validate/Exception.php'; 0254 throw new Zend_Validate_Exception("The maximum image height must be greater than or equal to the " 0255 . "minimum image height, but {$options['maxheight']} < {$this->_minwidth}"); 0256 } 0257 } 0258 0259 if (isset($options['maxwidth'])) { 0260 $this->_maxwidth = (int) $options['maxwidth']; 0261 } 0262 0263 if (isset($options['maxheight'])) { 0264 $this->_maxheight = (int) $options['maxheight']; 0265 } 0266 0267 return $this; 0268 } 0269 0270 /** 0271 * Sets the mimimum and maximum image width 0272 * 0273 * @param array $options The image width dimensions 0274 * @return Zend_Validate_File_ImageSize Provides a fluent interface 0275 */ 0276 public function setImageWidth($options) 0277 { 0278 $this->setImageMin($options); 0279 $this->setImageMax($options); 0280 0281 return $this; 0282 } 0283 0284 /** 0285 * Sets the mimimum and maximum image height 0286 * 0287 * @param array $options The image height dimensions 0288 * @return Zend_Validate_File_ImageSize Provides a fluent interface 0289 */ 0290 public function setImageHeight($options) 0291 { 0292 $this->setImageMin($options); 0293 $this->setImageMax($options); 0294 0295 return $this; 0296 } 0297 0298 /** 0299 * Defined by Zend_Validate_Interface 0300 * 0301 * Returns true if and only if the imagesize of $value is at least min and 0302 * not bigger than max 0303 * 0304 * @param string $value Real file to check for image size 0305 * @param array $file File data from Zend_File_Transfer 0306 * @return boolean 0307 */ 0308 public function isValid($value, $file = null) 0309 { 0310 // Is file readable ? 0311 // require_once 'Zend/Loader.php'; 0312 if (!Zend_Loader::isReadable($value)) { 0313 return $this->_throw($file, self::NOT_READABLE); 0314 } 0315 0316 $size = @getimagesize($value); 0317 $this->_setValue($file); 0318 0319 if (empty($size) or ($size[0] === 0) or ($size[1] === 0)) { 0320 return $this->_throw($file, self::NOT_DETECTED); 0321 } 0322 0323 $this->_width = $size[0]; 0324 $this->_height = $size[1]; 0325 if ($this->_width < $this->_minwidth) { 0326 $this->_throw($file, self::WIDTH_TOO_SMALL); 0327 } 0328 0329 if (($this->_maxwidth !== null) and ($this->_maxwidth < $this->_width)) { 0330 $this->_throw($file, self::WIDTH_TOO_BIG); 0331 } 0332 0333 if ($this->_height < $this->_minheight) { 0334 $this->_throw($file, self::HEIGHT_TOO_SMALL); 0335 } 0336 0337 if (($this->_maxheight !== null) and ($this->_maxheight < $this->_height)) { 0338 $this->_throw($file, self::HEIGHT_TOO_BIG); 0339 } 0340 0341 if (count($this->_messages) > 0) { 0342 return false; 0343 } 0344 0345 return true; 0346 } 0347 0348 /** 0349 * Throws an error of the given type 0350 * 0351 * @param string $file 0352 * @param string $errorType 0353 * @return false 0354 */ 0355 protected function _throw($file, $errorType) 0356 { 0357 if ($file !== null) { 0358 $this->_value = $file['name']; 0359 } 0360 0361 $this->_error($errorType); 0362 return false; 0363 } 0364 }