PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/DevApp/library/Zend/Validate/File/ImageSize.php

http://firephp.googlecode.com/
PHP | 353 lines | 166 code | 38 blank | 149 comment | 29 complexity | 0820a0d9f40b72490e0fd7bd8e60924c MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT, Apache-2.0
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: $
  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-2008 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. * Min limits the filesize, when used with max=null if is the maximum filesize
  106. * It also accepts an array with the keys 'min' and 'max'
  107. *
  108. * @param integer|array $max Maximum filesize
  109. * @param integer $max Maximum filesize
  110. * @return void
  111. */
  112. public function __construct($minwidth = 0, $minheight = 0, $maxwidth = null, $maxheight = null)
  113. {
  114. if (is_array($minwidth) === true) {
  115. if (isset($minwidth['maxheight']) === true) {
  116. $maxheight = $minwidth['maxheight'];
  117. }
  118. if (isset($minwidth['minheight']) === true) {
  119. $minheight = $minwidth['minheight'];
  120. }
  121. if (isset($minwidth['maxwidth']) === true) {
  122. $maxwidth = $minwidth['maxwidth'];
  123. }
  124. if (isset($minwidth['minwidth']) === true) {
  125. $minwidth = $minwidth['minwidth'];
  126. }
  127. if (isset($minwidth[0]) === true) {
  128. $maxheight = $minwidth[3];
  129. $maxwidth = $minwidth[2];
  130. $minheight = $minwidth[1];
  131. $minwidth = $minwidth[0];
  132. }
  133. }
  134. $this->setImageMin($minwidth, $minheight);
  135. $this->setImageMax($maxwidth, $maxheight);
  136. }
  137. /**
  138. * Returns the set minimum image sizes
  139. *
  140. * @return array
  141. */
  142. public function getImageMin()
  143. {
  144. return array($this->_minwidth, $this->_minheight);
  145. }
  146. /**
  147. * Returns the set maximum image sizes
  148. *
  149. * @return array
  150. */
  151. public function getImageMax()
  152. {
  153. return array($this->_maxwidth, $this->_maxheight);
  154. }
  155. /**
  156. * Returns the set image width sizes
  157. *
  158. * @return array
  159. */
  160. public function getImageWidth()
  161. {
  162. return array($this->_minwidth, $this->_maxwidth);
  163. }
  164. /**
  165. * Returns the set image height sizes
  166. *
  167. * @return array
  168. */
  169. public function getImageHeight()
  170. {
  171. return array($this->_minheight, $this->_maxheight);
  172. }
  173. /**
  174. * Sets the minimum image size
  175. *
  176. * @param integer $minwidth The minimum image width
  177. * @param integer $minheight The minimum image height
  178. * @throws Zend_Validate_Exception When minwidth is greater than maxwidth
  179. * @throws Zend_Validate_Exception When minheight is greater than maxheight
  180. * @return Zend_Validate_File_ImageSize Provides a fluent interface
  181. */
  182. public function setImageMin($minwidth, $minheight)
  183. {
  184. if (($this->_maxwidth !== null) and ($minwidth > $this->_maxwidth)) {
  185. require_once 'Zend/Validate/Exception.php';
  186. throw new Zend_Validate_Exception("The minimum image width must be less than or equal to the "
  187. . " maximum image width, but {$minwidth} > {$this->_maxwidth}");
  188. }
  189. if (($this->_maxheight !== null) and ($minheight > $this->_maxheight)) {
  190. require_once 'Zend/Validate/Exception.php';
  191. throw new Zend_Validate_Exception("The minimum image height must be less than or equal to the "
  192. . " maximum image height, but {$minheight} > {$this->_maxheight}");
  193. }
  194. $this->_minwidth = max(0, (integer) $minwidth);
  195. $this->_minheight = max(0, (integer) $minheight);
  196. return $this;
  197. }
  198. /**
  199. * Sets the maximum image size
  200. *
  201. * @param integer $maxwidth The maximum image width
  202. * @param integer $maxheight The maximum image height
  203. * @throws Zend_Validate_Exception When maxwidth is smaller than minwidth
  204. * @throws Zend_Validate_Exception When maxheight is smaller than minheight
  205. * @return Zend_Validate_StringLength Provides a fluent interface
  206. */
  207. public function setImageMax($maxwidth, $maxheight)
  208. {
  209. if ($maxwidth === null) {
  210. $tempwidth = null;
  211. } else if (($this->_minwidth !== null) and ($maxwidth < $this->_minwidth)) {
  212. require_once 'Zend/Validate/Exception.php';
  213. throw new Zend_Validate_Exception("The maximum image width must be greater than or equal to the "
  214. . "minimum image width, but {$maxwidth} < {$this->_minwidth}");
  215. } else {
  216. $tempwidth = (integer) $maxwidth;
  217. }
  218. if ($maxheight === null) {
  219. $tempheight = null;
  220. } else if (($this->_minheight !== null) and ($maxheight < $this->_minheight)) {
  221. require_once 'Zend/Validate/Exception.php';
  222. throw new Zend_Validate_Exception("The maximum image height must be greater than or equal to the "
  223. . "minimum image height, but {$maxheight} < {$this->_minwidth}");
  224. } else {
  225. $tempheight = (integer) $maxheight;
  226. }
  227. $this->_maxwidth = $tempwidth;
  228. $this->_maxheight = $tempheight;
  229. return $this;
  230. }
  231. /**
  232. * Sets the mimimum and maximum image width
  233. *
  234. * @param integer $minwidth The minimum image width
  235. * @param integer $maxwidth The maximum image width
  236. * @return Zend_Validate_File_ImageSize Provides a fluent interface
  237. */
  238. public function setImageWidth($minwidth, $maxwidth)
  239. {
  240. $this->setImageMin($minwidth, $this->_minheight);
  241. $this->setImageMax($maxwidth, $this->_maxheight);
  242. return $this;
  243. }
  244. /**
  245. * Sets the mimimum and maximum image height
  246. *
  247. * @param integer $minheight The minimum image height
  248. * @param integer $maxheight The maximum image height
  249. * @return Zend_Validate_File_ImageSize Provides a fluent interface
  250. */
  251. public function setImageHeight($minheight, $maxheight)
  252. {
  253. $this->setImageMin($this->_minwidth, $minheight);
  254. $this->setImageMax($this->_maxwidth, $maxheight);
  255. return $this;
  256. }
  257. /**
  258. * Defined by Zend_Validate_Interface
  259. *
  260. * Returns true if and only if the imagesize of $value is at least min and
  261. * not bigger than max
  262. *
  263. * @param string $value Real file to check for image size
  264. * @param array $file File data from Zend_File_Transfer
  265. * @return boolean
  266. */
  267. public function isValid($value, $file = null)
  268. {
  269. // Is file readable ?
  270. if (@is_readable($value) === false) {
  271. $this->_throw($file, self::NOT_READABLE);
  272. return false;
  273. }
  274. $size = @getimagesize($value);
  275. $this->_setValue($file);
  276. if (empty($size) or ($size[0] === 0) or ($size[1] === 0)) {
  277. $this->_throw($file, self::NOT_DETECTED);
  278. return false;
  279. }
  280. $this->_width = $size[0];
  281. $this->_height = $size[1];
  282. if ($this->_width < $this->_minwidth) {
  283. $this->_throw($file, self::WIDTH_TOO_SMALL);
  284. }
  285. if (($this->_maxwidth !== null) and ($this->_maxwidth < $this->_width)) {
  286. $this->_throw($file, self::WIDTH_TOO_BIG);
  287. }
  288. if ($this->_height < $this->_minheight) {
  289. $this->_throw($file, self::HEIGHT_TOO_SMALL);
  290. }
  291. if (($this->_maxheight !== null) and ($this->_maxheight < $this->_height)) {
  292. $this->_throw($file, self::HEIGHT_TOO_BIG);
  293. }
  294. if (count($this->_messages) > 0) {
  295. return false;
  296. } else {
  297. return true;
  298. }
  299. }
  300. /**
  301. * Throws an error of the given type
  302. *
  303. * @param string $file
  304. * @param string $errorType
  305. * @return false
  306. */
  307. protected function _throw($file, $errorType)
  308. {
  309. if ($file !== null) {
  310. $this->_value = $file['name'];
  311. }
  312. $this->_error($errorType);
  313. return false;
  314. }
  315. }