|
Moodle
2.2.1
http://www.collinsharper.com
|
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 => "File '%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 if (!is_array($options)) { 00131 $options = array('minwidth' => $options); 00132 } 00133 $argv = func_get_args(); 00134 array_shift($argv); 00135 $options['minheight'] = array_shift($argv); 00136 if (!empty($argv)) { 00137 $options['maxwidth'] = array_shift($argv); 00138 if (!empty($argv)) { 00139 $options['maxheight'] = array_shift($argv); 00140 } 00141 } 00142 } else if (!is_array($options)) { 00143 require_once 'Zend/Validate/Exception.php'; 00144 throw new Zend_Validate_Exception ('Invalid options to validator provided'); 00145 } 00146 00147 if (isset($options['minheight']) || isset($options['minwidth'])) { 00148 $this->setImageMin($options); 00149 } 00150 00151 if (isset($options['maxheight']) || isset($options['maxwidth'])) { 00152 $this->setImageMax($options); 00153 } 00154 } 00155 00161 public function getImageMin() 00162 { 00163 return array('minwidth' => $this->_minwidth, 'minheight' => $this->_minheight); 00164 } 00165 00171 public function getImageMax() 00172 { 00173 return array('maxwidth' => $this->_maxwidth, 'maxheight' => $this->_maxheight); 00174 } 00175 00181 public function getImageWidth() 00182 { 00183 return array('minwidth' => $this->_minwidth, 'maxwidth' => $this->_maxwidth); 00184 } 00185 00191 public function getImageHeight() 00192 { 00193 return array('minheight' => $this->_minheight, 'maxheight' => $this->_maxheight); 00194 } 00195 00204 public function setImageMin($options) 00205 { 00206 if (isset($options['minwidth'])) { 00207 if (($this->_maxwidth !== null) and ($options['minwidth'] > $this->_maxwidth)) { 00208 require_once 'Zend/Validate/Exception.php'; 00209 throw new Zend_Validate_Exception("The minimum image width must be less than or equal to the " 00210 . " maximum image width, but {$options['minwidth']} > {$this->_maxwidth}"); 00211 } 00212 } 00213 00214 if (isset($options['maxheight'])) { 00215 if (($this->_maxheight !== null) and ($options['minheight'] > $this->_maxheight)) { 00216 require_once 'Zend/Validate/Exception.php'; 00217 throw new Zend_Validate_Exception("The minimum image height must be less than or equal to the " 00218 . " maximum image height, but {$options['minheight']} > {$this->_maxheight}"); 00219 } 00220 } 00221 00222 if (isset($options['minwidth'])) { 00223 $this->_minwidth = (int) $options['minwidth']; 00224 } 00225 00226 if (isset($options['minheight'])) { 00227 $this->_minheight = (int) $options['minheight']; 00228 } 00229 00230 return $this; 00231 } 00232 00241 public function setImageMax($options) 00242 { 00243 if (isset($options['maxwidth'])) { 00244 if (($this->_minwidth !== null) and ($options['maxwidth'] < $this->_minwidth)) { 00245 require_once 'Zend/Validate/Exception.php'; 00246 throw new Zend_Validate_Exception("The maximum image width must be greater than or equal to the " 00247 . "minimum image width, but {$options['maxwidth']} < {$this->_minwidth}"); 00248 } 00249 } 00250 00251 if (isset($options['maxheight'])) { 00252 if (($this->_minheight !== null) and ($options['maxheight'] < $this->_minheight)) { 00253 require_once 'Zend/Validate/Exception.php'; 00254 throw new Zend_Validate_Exception("The maximum image height must be greater than or equal to the " 00255 . "minimum image height, but {$options['maxheight']} < {$this->_minwidth}"); 00256 } 00257 } 00258 00259 if (isset($options['maxwidth'])) { 00260 $this->_maxwidth = (int) $options['maxwidth']; 00261 } 00262 00263 if (isset($options['maxheight'])) { 00264 $this->_maxheight = (int) $options['maxheight']; 00265 } 00266 00267 return $this; 00268 } 00269 00276 public function setImageWidth($options) 00277 { 00278 $this->setImageMin($options); 00279 $this->setImageMax($options); 00280 00281 return $this; 00282 } 00283 00290 public function setImageHeight($options) 00291 { 00292 $this->setImageMin($options); 00293 $this->setImageMax($options); 00294 00295 return $this; 00296 } 00297 00308 public function isValid($value, $file = null) 00309 { 00310 // Is file readable ? 00311 require_once 'Zend/Loader.php'; 00312 if (!Zend_Loader::isReadable($value)) { 00313 return $this->_throw($file, self::NOT_READABLE); 00314 } 00315 00316 $size = @getimagesize($value); 00317 $this->_setValue($file); 00318 00319 if (empty($size) or ($size[0] === 0) or ($size[1] === 0)) { 00320 return $this->_throw($file, self::NOT_DETECTED); 00321 } 00322 00323 $this->_width = $size[0]; 00324 $this->_height = $size[1]; 00325 if ($this->_width < $this->_minwidth) { 00326 $this->_throw($file, self::WIDTH_TOO_SMALL); 00327 } 00328 00329 if (($this->_maxwidth !== null) and ($this->_maxwidth < $this->_width)) { 00330 $this->_throw($file, self::WIDTH_TOO_BIG); 00331 } 00332 00333 if ($this->_height < $this->_minheight) { 00334 $this->_throw($file, self::HEIGHT_TOO_SMALL); 00335 } 00336 00337 if (($this->_maxheight !== null) and ($this->_maxheight < $this->_height)) { 00338 $this->_throw($file, self::HEIGHT_TOO_BIG); 00339 } 00340 00341 if (count($this->_messages) > 0) { 00342 return false; 00343 } 00344 00345 return true; 00346 } 00347 00355 protected function _throw($file, $errorType) 00356 { 00357 if ($file !== null) { 00358 $this->_value = $file['name']; 00359 } 00360 00361 $this->_error($errorType); 00362 return false; 00363 } 00364 }