PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/monica/monica/vendor/zendframework/zendframework/library/Zend/Validator/File/ImageSize.php

https://bitbucket.org/alexandretaz/maniac_divers
PHP | 379 lines | 191 code | 43 blank | 145 comment | 31 complexity | e677ecb57c8d56702a506be8f7be84e4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  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
  8. */
  9. namespace Zend\Validator\File;
  10. use Zend\Stdlib\ErrorHandler;
  11. use Zend\Validator\AbstractValidator;
  12. use Zend\Validator\Exception;
  13. /**
  14. * Validator for the image size of a image file
  15. */
  16. class ImageSize extends AbstractValidator
  17. {
  18. /**
  19. * @const string Error constants
  20. */
  21. const WIDTH_TOO_BIG = 'fileImageSizeWidthTooBig';
  22. const WIDTH_TOO_SMALL = 'fileImageSizeWidthTooSmall';
  23. const HEIGHT_TOO_BIG = 'fileImageSizeHeightTooBig';
  24. const HEIGHT_TOO_SMALL = 'fileImageSizeHeightTooSmall';
  25. const NOT_DETECTED = 'fileImageSizeNotDetected';
  26. const NOT_READABLE = 'fileImageSizeNotReadable';
  27. /**
  28. * @var array Error message template
  29. */
  30. protected $messageTemplates = array(
  31. self::WIDTH_TOO_BIG => "Maximum allowed width for image should be '%maxwidth%' but '%width%' detected",
  32. self::WIDTH_TOO_SMALL => "Minimum expected width for image should be '%minwidth%' but '%width%' detected",
  33. self::HEIGHT_TOO_BIG => "Maximum allowed height for image should be '%maxheight%' but '%height%' detected",
  34. self::HEIGHT_TOO_SMALL => "Minimum expected height for image should be '%minheight%' but '%height%' detected",
  35. self::NOT_DETECTED => "The size of image could not be detected",
  36. self::NOT_READABLE => "File is not readable or does not exist",
  37. );
  38. /**
  39. * @var array Error message template variables
  40. */
  41. protected $messageVariables = array(
  42. 'minwidth' => array('options' => 'minWidth'),
  43. 'maxwidth' => array('options' => 'maxWidth'),
  44. 'minheight' => array('options' => 'minHeight'),
  45. 'maxheight' => array('options' => 'maxHeight'),
  46. 'width' => 'width',
  47. 'height' => 'height'
  48. );
  49. /**
  50. * Detected width
  51. *
  52. * @var integer
  53. */
  54. protected $width;
  55. /**
  56. * Detected height
  57. *
  58. * @var integer
  59. */
  60. protected $height;
  61. /**
  62. * Options for this validator
  63. *
  64. * @var array
  65. */
  66. protected $options = array(
  67. 'minWidth' => null, // Minimum image width
  68. 'maxWidth' => null, // Maximum image width
  69. 'minHeight' => null, // Minimum image height
  70. 'maxHeight' => null, // Maximum image height
  71. );
  72. /**
  73. * Sets validator options
  74. *
  75. * Accepts the following option keys:
  76. * - minheight
  77. * - minwidth
  78. * - maxheight
  79. * - maxwidth
  80. *
  81. * @param array|\Traversable $options
  82. */
  83. public function __construct($options = null)
  84. {
  85. if (1 < func_num_args()) {
  86. if (!is_array($options)) {
  87. $options = array('minWidth' => $options);
  88. }
  89. $argv = func_get_args();
  90. array_shift($argv);
  91. $options['minHeight'] = array_shift($argv);
  92. if (!empty($argv)) {
  93. $options['maxWidth'] = array_shift($argv);
  94. if (!empty($argv)) {
  95. $options['maxHeight'] = array_shift($argv);
  96. }
  97. }
  98. }
  99. parent::__construct($options);
  100. }
  101. /**
  102. * Returns the minimum allowed width
  103. *
  104. * @return integer
  105. */
  106. public function getMinWidth()
  107. {
  108. return $this->options['minWidth'];
  109. }
  110. /**
  111. * Sets the minimum allowed width
  112. *
  113. * @param integer $minWidth
  114. * @return ImageSize Provides a fluid interface
  115. * @throws Exception\InvalidArgumentException When minwidth is greater than maxwidth
  116. */
  117. public function setMinWidth($minWidth)
  118. {
  119. if (($this->getMaxWidth() !== null) && ($minWidth > $this->getMaxWidth())) {
  120. throw new Exception\InvalidArgumentException("The minimum image width must be less than or equal to the "
  121. . " maximum image width, but {$minWidth} > {$this->getMaxWidth()}");
  122. }
  123. $this->options['minWidth'] = (int) $minWidth;
  124. return $this;
  125. }
  126. /**
  127. * Returns the maximum allowed width
  128. *
  129. * @return integer
  130. */
  131. public function getMaxWidth()
  132. {
  133. return $this->options['maxWidth'];
  134. }
  135. /**
  136. * Sets the maximum allowed width
  137. *
  138. * @param integer $maxWidth
  139. * @return ImageSize Provides a fluid interface
  140. * @throws Exception\InvalidArgumentException When maxwidth is less than minwidth
  141. */
  142. public function setMaxWidth($maxWidth)
  143. {
  144. if (($this->getMinWidth() !== null) && ($maxWidth < $this->getMinWidth())) {
  145. throw new Exception\InvalidArgumentException("The maximum image width must be greater than or equal to the "
  146. . "minimum image width, but {$maxWidth} < {$this->getMinWidth()}");
  147. }
  148. $this->options['maxWidth'] = (int) $maxWidth;
  149. return $this;
  150. }
  151. /**
  152. * Returns the minimum allowed height
  153. *
  154. * @return integer
  155. */
  156. public function getMinHeight()
  157. {
  158. return $this->options['minHeight'];
  159. }
  160. /**
  161. * Sets the minimum allowed height
  162. *
  163. * @param integer $minHeight
  164. * @return ImageSize Provides a fluid interface
  165. * @throws Exception\InvalidArgumentException When minheight is greater than maxheight
  166. */
  167. public function setMinHeight($minHeight)
  168. {
  169. if (($this->getMaxHeight() !== null) && ($minHeight > $this->getMaxHeight())) {
  170. throw new Exception\InvalidArgumentException("The minimum image height must be less than or equal to the "
  171. . " maximum image height, but {$minHeight} > {$this->getMaxHeight()}");
  172. }
  173. $this->options['minHeight'] = (int) $minHeight;
  174. return $this;
  175. }
  176. /**
  177. * Returns the maximum allowed height
  178. *
  179. * @return integer
  180. */
  181. public function getMaxHeight()
  182. {
  183. return $this->options['maxHeight'];
  184. }
  185. /**
  186. * Sets the maximum allowed height
  187. *
  188. * @param integer $maxHeight
  189. * @return ImageSize Provides a fluid interface
  190. * @throws Exception\InvalidArgumentException When maxheight is less than minheight
  191. */
  192. public function setMaxHeight($maxHeight)
  193. {
  194. if (($this->getMinHeight() !== null) && ($maxHeight < $this->getMinHeight())) {
  195. throw new Exception\InvalidArgumentException("The maximum image height must be greater than or equal to the "
  196. . "minimum image height, but {$maxHeight} < {$this->getMinHeight()}");
  197. }
  198. $this->options['maxHeight'] = (int) $maxHeight;
  199. return $this;
  200. }
  201. /**
  202. * Returns the set minimum image sizes
  203. *
  204. * @return array
  205. */
  206. public function getImageMin()
  207. {
  208. return array('minWidth' => $this->getMinWidth(), 'minHeight' => $this->getMinHeight());
  209. }
  210. /**
  211. * Returns the set maximum image sizes
  212. *
  213. * @return array
  214. */
  215. public function getImageMax()
  216. {
  217. return array('maxWidth' => $this->getMaxWidth(), 'maxHeight' => $this->getMaxHeight());
  218. }
  219. /**
  220. * Returns the set image width sizes
  221. *
  222. * @return array
  223. */
  224. public function getImageWidth()
  225. {
  226. return array('minWidth' => $this->getMinWidth(), 'maxWidth' => $this->getMaxWidth());
  227. }
  228. /**
  229. * Returns the set image height sizes
  230. *
  231. * @return array
  232. */
  233. public function getImageHeight()
  234. {
  235. return array('minHeight' => $this->getMinHeight(), 'maxHeight' => $this->getMaxHeight());
  236. }
  237. /**
  238. * Sets the minimum image size
  239. *
  240. * @param array $options The minimum image dimensions
  241. * @return ImageSize Provides a fluent interface
  242. */
  243. public function setImageMin($options)
  244. {
  245. $this->setOptions($options);
  246. return $this;
  247. }
  248. /**
  249. * Sets the maximum image size
  250. *
  251. * @param array|\Traversable $options The maximum image dimensions
  252. * @return ImageSize Provides a fluent interface
  253. */
  254. public function setImageMax($options)
  255. {
  256. $this->setOptions($options);
  257. return $this;
  258. }
  259. /**
  260. * Sets the minimum and maximum image width
  261. *
  262. * @param array $options The image width dimensions
  263. * @return ImageSize Provides a fluent interface
  264. */
  265. public function setImageWidth($options)
  266. {
  267. $this->setImageMin($options);
  268. $this->setImageMax($options);
  269. return $this;
  270. }
  271. /**
  272. * Sets the minimum and maximum image height
  273. *
  274. * @param array $options The image height dimensions
  275. * @return ImageSize Provides a fluent interface
  276. */
  277. public function setImageHeight($options)
  278. {
  279. $this->setImageMin($options);
  280. $this->setImageMax($options);
  281. return $this;
  282. }
  283. /**
  284. * Returns true if and only if the image size of $value is at least min and
  285. * not bigger than max
  286. *
  287. * @param string|array $value Real file to check for image size
  288. * @return bool
  289. */
  290. public function isValid($value)
  291. {
  292. if (is_array($value)) {
  293. if (!isset($value['tmp_name']) || !isset($value['name'])) {
  294. throw new Exception\InvalidArgumentException(
  295. 'Value array must be in $_FILES format'
  296. );
  297. }
  298. $file = $value['tmp_name'];
  299. $filename = $value['name'];
  300. } else {
  301. $file = $value;
  302. $filename = basename($file);
  303. }
  304. $this->setValue($filename);
  305. // Is file readable ?
  306. if (false === stream_resolve_include_path($file)) {
  307. $this->error(self::NOT_READABLE);
  308. return false;
  309. }
  310. ErrorHandler::start();
  311. $size = getimagesize($file);
  312. ErrorHandler::stop();
  313. if (empty($size) || ($size[0] === 0) || ($size[1] === 0)) {
  314. $this->error(self::NOT_DETECTED);
  315. return false;
  316. }
  317. $this->width = $size[0];
  318. $this->height = $size[1];
  319. if ($this->width < $this->getMinWidth()) {
  320. $this->error(self::WIDTH_TOO_SMALL);
  321. }
  322. if (($this->getMaxWidth() !== null) && ($this->getMaxWidth() < $this->width)) {
  323. $this->error(self::WIDTH_TOO_BIG);
  324. }
  325. if ($this->height < $this->getMinHeight()) {
  326. $this->error(self::HEIGHT_TOO_SMALL);
  327. }
  328. if (($this->getMaxHeight() !== null) && ($this->getMaxHeight() < $this->height)) {
  329. $this->error(self::HEIGHT_TOO_BIG);
  330. }
  331. if (count($this->getMessages()) > 0) {
  332. return false;
  333. }
  334. return true;
  335. }
  336. }