1: <?php
2: /**
3: * Wei Framework
4: *
5: * @copyright Copyright (c) 2008-2013 Twin Huang
6: * @license http://opensource.org/licenses/mit-license.php MIT License
7: */
8:
9: namespace Wei\Validator;
10:
11: /**
12: * Check if the input is valid image
13: *
14: * @author Twin Huang <twinhuang@qq.com>
15: */
16: class Image extends File
17: {
18: protected $notDetectedMessage = '%name% is not a valid image or the size of the image could not be detected';
19:
20: protected $widthTooBigMessage = '%name% width is too big (%width%px), allowed maximum width is %maxWidth%px';
21:
22: protected $widthTooSmallMessage = '%name% width is too small (%width%px), expected minimum width is %minWidth%px';
23:
24: protected $heightTooBigMessage = '%name% height is too big (%height%px), allowed maximum height is %maxHeight%px';
25:
26: protected $heightTooSmallMessage = '%name% height is too small (%height%px), expected minimum height is %minHeight%px';
27:
28: protected $maxWidth;
29:
30: protected $minWidth;
31:
32: protected $maxHeight;
33:
34: protected $minHeight;
35:
36: /**
37: * The detected width of image
38: *
39: * @var int
40: */
41: protected $width;
42:
43: /**
44: * The detected height of image
45: *
46: * @var int
47: */
48: protected $height;
49:
50: /**
51: * {@inheritdoc}
52: */
53: protected function doValidate($input)
54: {
55: parent::doValidate($input);
56: if ($this->hasError('notString') || $this->hasError('notFound')) {
57: return false;
58: }
59:
60: // Receive the real file path resolved by parent class
61: $file = $this->file;
62:
63: $size = @getimagesize($file);
64: if (false === $size) {
65: $this->addError('notDetected');
66: return false;
67: }
68:
69: $this->width = $size[0];
70: $this->height = $size[1];
71:
72: if ($this->maxWidth && $this->maxWidth < $this->width) {
73: $this->addError('widthTooBig');
74: }
75:
76: if ($this->minWidth && $this->minWidth > $this->width) {
77: $this->addError('widthTooSmall');
78: }
79:
80: if ($this->maxHeight && $this->maxHeight < $this->height) {
81: $this->addError('heightTooBig');
82: }
83:
84: if ($this->minHeight && $this->minHeight > $this->height) {
85: $this->addError('heightTooSmall');
86: }
87:
88: return !$this->errors;
89: }
90: }
91: