4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Constraints;
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;
20 * Validates whether a value is a valid image file and is valid
21 * against minWidth, maxWidth, minHeight and maxHeight constraints.
23 * @author Benjamin Dulau <benjamin.dulau@gmail.com>
24 * @author Bernhard Schussek <bschussek@gmail.com>
26 class ImageValidator extends FileValidator
31 public function validate($value, Constraint $constraint)
33 if (!$constraint instanceof Image) {
34 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Image');
37 $violations = count($this->context->getViolations());
39 parent::validate($value, $constraint);
41 $failed = count($this->context->getViolations()) !== $violations;
43 if ($failed || null === $value || '' === $value) {
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) {
56 $size = @getimagesize($value);
58 if (empty($size) || (0 === $size[0]) || (0 === $size[1])) {
59 $this->context->buildViolation($constraint->sizeNotDetectedMessage)
60 ->setCode(Image::SIZE_NOT_DETECTED_ERROR)
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));
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)
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));
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)
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));
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)
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));
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)
131 $pixels = $width * $height;
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));
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)
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));
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)
165 $ratio = round($width / $height, 2);
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));
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)
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));
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)
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)
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)
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)
219 if ($constraint->detectCorrupted) {
220 if (!function_exists('imagecreatefromstring')) {
221 throw new RuntimeException('Corrupted images detection requires installed and enabled GD extension');
224 $resource = @imagecreatefromstring(file_get_contents($value));
226 if (false === $resource) {
227 $this->context->buildViolation($constraint->corruptedMessage)
228 ->setCode(Image::CORRUPTED_IMAGE_ERROR)
234 imagedestroy($resource);