PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Zend/Validate/File/ImageSize.php

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