/package/app/app/vendor/ZendFramework/library/Zend/Validate/File/ImageSize.php

https://github.com/richhl/kalturaCE · PHP · 366 lines · 175 code · 42 blank · 149 comment · 34 complexity · 6b9f7df61f51e1e93102031516326536 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 18148 2009-09-16 19:27:43Z thomas $
  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. if ($options instanceof Zend_Config) {
  117. $options = $options->toArray();
  118. } elseif (1 < func_num_args()) {
  119. // @todo: Preperation for 2.0... needs to be cleared with the dev-team
  120. // trigger_error('Multiple constructor options are deprecated in favor of a single options array', E_USER_NOTICE);
  121. if (!is_array($options)) {
  122. $options = array('minwidth' => $options);
  123. }
  124. $argv = func_get_args();
  125. array_shift($argv);
  126. $options['minheight'] = array_shift($argv);
  127. if (!empty($argv)) {
  128. $options['maxwidth'] = array_shift($argv);
  129. if (!empty($argv)) {
  130. $options['maxheight'] = array_shift($argv);
  131. }
  132. }
  133. } else if (!is_array($options)) {
  134. require_once 'Zend/Validate/Exception.php';
  135. throw new Zend_Validate_Exception ('Invalid options to validator provided');
  136. }
  137. if (isset($options['minheight']) || isset($options['minwidth'])) {
  138. $this->setImageMin($options);
  139. }
  140. if (isset($options['maxheight']) || isset($options['maxwidth'])) {
  141. $this->setImageMax($options);
  142. }
  143. }
  144. /**
  145. * Returns the set minimum image sizes
  146. *
  147. * @return array
  148. */
  149. public function getImageMin()
  150. {
  151. return array('minwidth' => $this->_minwidth, 'minheight' => $this->_minheight);
  152. }
  153. /**
  154. * Returns the set maximum image sizes
  155. *
  156. * @return array
  157. */
  158. public function getImageMax()
  159. {
  160. return array('maxwidth' => $this->_maxwidth, 'maxheight' => $this->_maxheight);
  161. }
  162. /**
  163. * Returns the set image width sizes
  164. *
  165. * @return array
  166. */
  167. public function getImageWidth()
  168. {
  169. return array('minwidth' => $this->_minwidth, 'maxwidth' => $this->_maxwidth);
  170. }
  171. /**
  172. * Returns the set image height sizes
  173. *
  174. * @return array
  175. */
  176. public function getImageHeight()
  177. {
  178. return array('minheight' => $this->_minheight, 'maxheight' => $this->_maxheight);
  179. }
  180. /**
  181. * Sets the minimum image size
  182. *
  183. * @param array $options The minimum image dimensions
  184. * @throws Zend_Validate_Exception When minwidth is greater than maxwidth
  185. * @throws Zend_Validate_Exception When minheight is greater than maxheight
  186. * @return Zend_Validate_File_ImageSize Provides a fluent interface
  187. */
  188. public function setImageMin($options)
  189. {
  190. if (isset($options['minwidth'])) {
  191. if (($this->_maxwidth !== null) and ($options['minwidth'] > $this->_maxwidth)) {
  192. require_once 'Zend/Validate/Exception.php';
  193. throw new Zend_Validate_Exception("The minimum image width must be less than or equal to the "
  194. . " maximum image width, but {$options['minwidth']} > {$this->_maxwidth}");
  195. }
  196. }
  197. if (isset($options['maxheight'])) {
  198. if (($this->_maxheight !== null) and ($options['minheight'] > $this->_maxheight)) {
  199. require_once 'Zend/Validate/Exception.php';
  200. throw new Zend_Validate_Exception("The minimum image height must be less than or equal to the "
  201. . " maximum image height, but {$options['minheight']} > {$this->_maxheight}");
  202. }
  203. }
  204. if (isset($options['minwidth'])) {
  205. $this->_minwidth = (int) $options['minwidth'];
  206. }
  207. if (isset($options['minheight'])) {
  208. $this->_minheight = (int) $options['minheight'];
  209. }
  210. return $this;
  211. }
  212. /**
  213. * Sets the maximum image size
  214. *
  215. * @param array $options The maximum image dimensions
  216. * @throws Zend_Validate_Exception When maxwidth is smaller than minwidth
  217. * @throws Zend_Validate_Exception When maxheight is smaller than minheight
  218. * @return Zend_Validate_StringLength Provides a fluent interface
  219. */
  220. public function setImageMax($options)
  221. {
  222. if (isset($options['maxwidth'])) {
  223. if (($this->_minwidth !== null) and ($options['maxwidth'] < $this->_minwidth)) {
  224. require_once 'Zend/Validate/Exception.php';
  225. throw new Zend_Validate_Exception("The maximum image width must be greater than or equal to the "
  226. . "minimum image width, but {$options['maxwidth']} < {$this->_minwidth}");
  227. }
  228. }
  229. if (isset($options['maxheight'])) {
  230. if (($this->_minheight !== null) and ($options['maxheight'] < $this->_minheight)) {
  231. require_once 'Zend/Validate/Exception.php';
  232. throw new Zend_Validate_Exception("The maximum image height must be greater than or equal to the "
  233. . "minimum image height, but {$options['maxheight']} < {$this->_minwidth}");
  234. }
  235. }
  236. if (isset($options['maxwidth'])) {
  237. $this->_maxwidth = (int) $options['maxwidth'];
  238. }
  239. if (isset($options['maxheight'])) {
  240. $this->_maxheight = (int) $options['maxheight'];
  241. }
  242. return $this;
  243. }
  244. /**
  245. * Sets the mimimum and maximum image width
  246. *
  247. * @param array $options The image width dimensions
  248. * @return Zend_Validate_File_ImageSize Provides a fluent interface
  249. */
  250. public function setImageWidth($options)
  251. {
  252. $this->setImageMin($options);
  253. $this->setImageMax($options);
  254. return $this;
  255. }
  256. /**
  257. * Sets the mimimum and maximum image height
  258. *
  259. * @param array $options The image height dimensions
  260. * @return Zend_Validate_File_ImageSize Provides a fluent interface
  261. */
  262. public function setImageHeight($options)
  263. {
  264. $this->setImageMin($options);
  265. $this->setImageMax($options);
  266. return $this;
  267. }
  268. /**
  269. * Defined by Zend_Validate_Interface
  270. *
  271. * Returns true if and only if the imagesize of $value is at least min and
  272. * not bigger than max
  273. *
  274. * @param string $value Real file to check for image size
  275. * @param array $file File data from Zend_File_Transfer
  276. * @return boolean
  277. */
  278. public function isValid($value, $file = null)
  279. {
  280. // Is file readable ?
  281. require_once 'Zend/Loader.php';
  282. if (!Zend_Loader::isReadable($value)) {
  283. return $this->_throw($file, self::NOT_READABLE);
  284. }
  285. $size = @getimagesize($value);
  286. $this->_setValue($file);
  287. if (empty($size) or ($size[0] === 0) or ($size[1] === 0)) {
  288. return $this->_throw($file, self::NOT_DETECTED);
  289. }
  290. $this->_width = $size[0];
  291. $this->_height = $size[1];
  292. if ($this->_width < $this->_minwidth) {
  293. $this->_throw($file, self::WIDTH_TOO_SMALL);
  294. }
  295. if (($this->_maxwidth !== null) and ($this->_maxwidth < $this->_width)) {
  296. $this->_throw($file, self::WIDTH_TOO_BIG);
  297. }
  298. if ($this->_height < $this->_minheight) {
  299. $this->_throw($file, self::HEIGHT_TOO_SMALL);
  300. }
  301. if (($this->_maxheight !== null) and ($this->_maxheight < $this->_height)) {
  302. $this->_throw($file, self::HEIGHT_TOO_BIG);
  303. }
  304. if (count($this->_messages) > 0) {
  305. return false;
  306. }
  307. return true;
  308. }
  309. /**
  310. * Throws an error of the given type
  311. *
  312. * @param string $file
  313. * @param string $errorType
  314. * @return false
  315. */
  316. protected function _throw($file, $errorType)
  317. {
  318. if ($file !== null) {
  319. $this->_value = $file['name'];
  320. }
  321. $this->_error($errorType);
  322. return false;
  323. }
  324. }