Mercurial > hg > cmmr2012-drupal-site
comparison vendor/symfony/validator/Constraints/ImageValidator.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of the Symfony package. | |
5 * | |
6 * (c) Fabien Potencier <fabien@symfony.com> | |
7 * | |
8 * For the full copyright and license information, please view the LICENSE | |
9 * file that was distributed with this source code. | |
10 */ | |
11 | |
12 namespace Symfony\Component\Validator\Constraints; | |
13 | |
14 use Symfony\Component\Validator\Constraint; | |
15 use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | |
16 use Symfony\Component\Validator\Exception\RuntimeException; | |
17 use Symfony\Component\Validator\Exception\UnexpectedTypeException; | |
18 | |
19 /** | |
20 * Validates whether a value is a valid image file and is valid | |
21 * against minWidth, maxWidth, minHeight and maxHeight constraints. | |
22 * | |
23 * @author Benjamin Dulau <benjamin.dulau@gmail.com> | |
24 * @author Bernhard Schussek <bschussek@gmail.com> | |
25 */ | |
26 class ImageValidator extends FileValidator | |
27 { | |
28 /** | |
29 * {@inheritdoc} | |
30 */ | |
31 public function validate($value, Constraint $constraint) | |
32 { | |
33 if (!$constraint instanceof Image) { | |
34 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Image'); | |
35 } | |
36 | |
37 $violations = count($this->context->getViolations()); | |
38 | |
39 parent::validate($value, $constraint); | |
40 | |
41 $failed = count($this->context->getViolations()) !== $violations; | |
42 | |
43 if ($failed || null === $value || '' === $value) { | |
44 return; | |
45 } | |
46 | |
47 if (null === $constraint->minWidth && null === $constraint->maxWidth | |
48 && null === $constraint->minHeight && null === $constraint->maxHeight | |
49 && null === $constraint->minPixels && null === $constraint->maxPixels | |
50 && null === $constraint->minRatio && null === $constraint->maxRatio | |
51 && $constraint->allowSquare && $constraint->allowLandscape && $constraint->allowPortrait | |
52 && !$constraint->detectCorrupted) { | |
53 return; | |
54 } | |
55 | |
56 $size = @getimagesize($value); | |
57 | |
58 if (empty($size) || (0 === $size[0]) || (0 === $size[1])) { | |
59 $this->context->buildViolation($constraint->sizeNotDetectedMessage) | |
60 ->setCode(Image::SIZE_NOT_DETECTED_ERROR) | |
61 ->addViolation(); | |
62 | |
63 return; | |
64 } | |
65 | |
66 $width = $size[0]; | |
67 $height = $size[1]; | |
68 | |
69 if ($constraint->minWidth) { | |
70 if (!ctype_digit((string) $constraint->minWidth)) { | |
71 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum width.', $constraint->minWidth)); | |
72 } | |
73 | |
74 if ($width < $constraint->minWidth) { | |
75 $this->context->buildViolation($constraint->minWidthMessage) | |
76 ->setParameter('{{ width }}', $width) | |
77 ->setParameter('{{ min_width }}', $constraint->minWidth) | |
78 ->setCode(Image::TOO_NARROW_ERROR) | |
79 ->addViolation(); | |
80 | |
81 return; | |
82 } | |
83 } | |
84 | |
85 if ($constraint->maxWidth) { | |
86 if (!ctype_digit((string) $constraint->maxWidth)) { | |
87 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum width.', $constraint->maxWidth)); | |
88 } | |
89 | |
90 if ($width > $constraint->maxWidth) { | |
91 $this->context->buildViolation($constraint->maxWidthMessage) | |
92 ->setParameter('{{ width }}', $width) | |
93 ->setParameter('{{ max_width }}', $constraint->maxWidth) | |
94 ->setCode(Image::TOO_WIDE_ERROR) | |
95 ->addViolation(); | |
96 | |
97 return; | |
98 } | |
99 } | |
100 | |
101 if ($constraint->minHeight) { | |
102 if (!ctype_digit((string) $constraint->minHeight)) { | |
103 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum height', $constraint->minHeight)); | |
104 } | |
105 | |
106 if ($height < $constraint->minHeight) { | |
107 $this->context->buildViolation($constraint->minHeightMessage) | |
108 ->setParameter('{{ height }}', $height) | |
109 ->setParameter('{{ min_height }}', $constraint->minHeight) | |
110 ->setCode(Image::TOO_LOW_ERROR) | |
111 ->addViolation(); | |
112 | |
113 return; | |
114 } | |
115 } | |
116 | |
117 if ($constraint->maxHeight) { | |
118 if (!ctype_digit((string) $constraint->maxHeight)) { | |
119 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum height', $constraint->maxHeight)); | |
120 } | |
121 | |
122 if ($height > $constraint->maxHeight) { | |
123 $this->context->buildViolation($constraint->maxHeightMessage) | |
124 ->setParameter('{{ height }}', $height) | |
125 ->setParameter('{{ max_height }}', $constraint->maxHeight) | |
126 ->setCode(Image::TOO_HIGH_ERROR) | |
127 ->addViolation(); | |
128 } | |
129 } | |
130 | |
131 $pixels = $width * $height; | |
132 | |
133 if (null !== $constraint->minPixels) { | |
134 if (!ctype_digit((string) $constraint->minPixels)) { | |
135 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum amount of pixels', $constraint->minPixels)); | |
136 } | |
137 | |
138 if ($pixels < $constraint->minPixels) { | |
139 $this->context->buildViolation($constraint->minPixelsMessage) | |
140 ->setParameter('{{ pixels }}', $pixels) | |
141 ->setParameter('{{ min_pixels }}', $constraint->minPixels) | |
142 ->setParameter('{{ height }}', $height) | |
143 ->setParameter('{{ width }}', $width) | |
144 ->setCode(Image::TOO_FEW_PIXEL_ERROR) | |
145 ->addViolation(); | |
146 } | |
147 } | |
148 | |
149 if (null !== $constraint->maxPixels) { | |
150 if (!ctype_digit((string) $constraint->maxPixels)) { | |
151 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum amount of pixels', $constraint->maxPixels)); | |
152 } | |
153 | |
154 if ($pixels > $constraint->maxPixels) { | |
155 $this->context->buildViolation($constraint->maxPixelsMessage) | |
156 ->setParameter('{{ pixels }}', $pixels) | |
157 ->setParameter('{{ max_pixels }}', $constraint->maxPixels) | |
158 ->setParameter('{{ height }}', $height) | |
159 ->setParameter('{{ width }}', $width) | |
160 ->setCode(Image::TOO_MANY_PIXEL_ERROR) | |
161 ->addViolation(); | |
162 } | |
163 } | |
164 | |
165 $ratio = round($width / $height, 2); | |
166 | |
167 if (null !== $constraint->minRatio) { | |
168 if (!is_numeric((string) $constraint->minRatio)) { | |
169 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum ratio', $constraint->minRatio)); | |
170 } | |
171 | |
172 if ($ratio < $constraint->minRatio) { | |
173 $this->context->buildViolation($constraint->minRatioMessage) | |
174 ->setParameter('{{ ratio }}', $ratio) | |
175 ->setParameter('{{ min_ratio }}', $constraint->minRatio) | |
176 ->setCode(Image::RATIO_TOO_SMALL_ERROR) | |
177 ->addViolation(); | |
178 } | |
179 } | |
180 | |
181 if (null !== $constraint->maxRatio) { | |
182 if (!is_numeric((string) $constraint->maxRatio)) { | |
183 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum ratio', $constraint->maxRatio)); | |
184 } | |
185 | |
186 if ($ratio > $constraint->maxRatio) { | |
187 $this->context->buildViolation($constraint->maxRatioMessage) | |
188 ->setParameter('{{ ratio }}', $ratio) | |
189 ->setParameter('{{ max_ratio }}', $constraint->maxRatio) | |
190 ->setCode(Image::RATIO_TOO_BIG_ERROR) | |
191 ->addViolation(); | |
192 } | |
193 } | |
194 | |
195 if (!$constraint->allowSquare && $width == $height) { | |
196 $this->context->buildViolation($constraint->allowSquareMessage) | |
197 ->setParameter('{{ width }}', $width) | |
198 ->setParameter('{{ height }}', $height) | |
199 ->setCode(Image::SQUARE_NOT_ALLOWED_ERROR) | |
200 ->addViolation(); | |
201 } | |
202 | |
203 if (!$constraint->allowLandscape && $width > $height) { | |
204 $this->context->buildViolation($constraint->allowLandscapeMessage) | |
205 ->setParameter('{{ width }}', $width) | |
206 ->setParameter('{{ height }}', $height) | |
207 ->setCode(Image::LANDSCAPE_NOT_ALLOWED_ERROR) | |
208 ->addViolation(); | |
209 } | |
210 | |
211 if (!$constraint->allowPortrait && $width < $height) { | |
212 $this->context->buildViolation($constraint->allowPortraitMessage) | |
213 ->setParameter('{{ width }}', $width) | |
214 ->setParameter('{{ height }}', $height) | |
215 ->setCode(Image::PORTRAIT_NOT_ALLOWED_ERROR) | |
216 ->addViolation(); | |
217 } | |
218 | |
219 if ($constraint->detectCorrupted) { | |
220 if (!function_exists('imagecreatefromstring')) { | |
221 throw new RuntimeException('Corrupted images detection requires installed and enabled GD extension'); | |
222 } | |
223 | |
224 $resource = @imagecreatefromstring(file_get_contents($value)); | |
225 | |
226 if (false === $resource) { | |
227 $this->context->buildViolation($constraint->corruptedMessage) | |
228 ->setCode(Image::CORRUPTED_IMAGE_ERROR) | |
229 ->addViolation(); | |
230 | |
231 return; | |
232 } | |
233 | |
234 imagedestroy($resource); | |
235 } | |
236 } | |
237 } |