PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/libraries/3rdparty/Zend/Validate/File/ImageSize.php

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