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->minRatio && null === $constraint->maxRatio
50 && $constraint->allowSquare && $constraint->allowLandscape && $constraint->allowPortrait
51 && !$constraint->detectCorrupted) {
55 $size = @getimagesize($value);
57 if (empty($size) || ($size[0] === 0) || ($size[1] === 0)) {
58 $this->context->buildViolation($constraint->sizeNotDetectedMessage)
59 ->setCode(Image::SIZE_NOT_DETECTED_ERROR)
68 if ($constraint->minWidth) {
69 if (!ctype_digit((string) $constraint->minWidth)) {
70 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum width', $constraint->minWidth));
73 if ($width < $constraint->minWidth) {
74 $this->context->buildViolation($constraint->minWidthMessage)
75 ->setParameter('{{ width }}', $width)
76 ->setParameter('{{ min_width }}', $constraint->minWidth)
77 ->setCode(Image::TOO_NARROW_ERROR)
84 if ($constraint->maxWidth) {
85 if (!ctype_digit((string) $constraint->maxWidth)) {
86 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum width', $constraint->maxWidth));
89 if ($width > $constraint->maxWidth) {
90 $this->context->buildViolation($constraint->maxWidthMessage)
91 ->setParameter('{{ width }}', $width)
92 ->setParameter('{{ max_width }}', $constraint->maxWidth)
93 ->setCode(Image::TOO_WIDE_ERROR)
100 if ($constraint->minHeight) {
101 if (!ctype_digit((string) $constraint->minHeight)) {
102 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum height', $constraint->minHeight));
105 if ($height < $constraint->minHeight) {
106 $this->context->buildViolation($constraint->minHeightMessage)
107 ->setParameter('{{ height }}', $height)
108 ->setParameter('{{ min_height }}', $constraint->minHeight)
109 ->setCode(Image::TOO_LOW_ERROR)
116 if ($constraint->maxHeight) {
117 if (!ctype_digit((string) $constraint->maxHeight)) {
118 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum height', $constraint->maxHeight));
121 if ($height > $constraint->maxHeight) {
122 $this->context->buildViolation($constraint->maxHeightMessage)
123 ->setParameter('{{ height }}', $height)
124 ->setParameter('{{ max_height }}', $constraint->maxHeight)
125 ->setCode(Image::TOO_HIGH_ERROR)
130 $ratio = round($width / $height, 2);
132 if (null !== $constraint->minRatio) {
133 if (!is_numeric((string) $constraint->minRatio)) {
134 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum ratio', $constraint->minRatio));
137 if ($ratio < $constraint->minRatio) {
138 $this->context->buildViolation($constraint->minRatioMessage)
139 ->setParameter('{{ ratio }}', $ratio)
140 ->setParameter('{{ min_ratio }}', $constraint->minRatio)
141 ->setCode(Image::RATIO_TOO_SMALL_ERROR)
146 if (null !== $constraint->maxRatio) {
147 if (!is_numeric((string) $constraint->maxRatio)) {
148 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum ratio', $constraint->maxRatio));
151 if ($ratio > $constraint->maxRatio) {
152 $this->context->buildViolation($constraint->maxRatioMessage)
153 ->setParameter('{{ ratio }}', $ratio)
154 ->setParameter('{{ max_ratio }}', $constraint->maxRatio)
155 ->setCode(Image::RATIO_TOO_BIG_ERROR)
160 if (!$constraint->allowSquare && $width == $height) {
161 $this->context->buildViolation($constraint->allowSquareMessage)
162 ->setParameter('{{ width }}', $width)
163 ->setParameter('{{ height }}', $height)
164 ->setCode(Image::SQUARE_NOT_ALLOWED_ERROR)
168 if (!$constraint->allowLandscape && $width > $height) {
169 $this->context->buildViolation($constraint->allowLandscapeMessage)
170 ->setParameter('{{ width }}', $width)
171 ->setParameter('{{ height }}', $height)
172 ->setCode(Image::LANDSCAPE_NOT_ALLOWED_ERROR)
176 if (!$constraint->allowPortrait && $width < $height) {
177 $this->context->buildViolation($constraint->allowPortraitMessage)
178 ->setParameter('{{ width }}', $width)
179 ->setParameter('{{ height }}', $height)
180 ->setCode(Image::PORTRAIT_NOT_ALLOWED_ERROR)
184 if ($constraint->detectCorrupted) {
185 if (!function_exists('imagecreatefromstring')) {
186 throw new RuntimeException('Corrupted images detection requires installed and enabled GD extension');
189 $resource = @imagecreatefromstring(file_get_contents($value));
191 if (false === $resource) {
192 $this->context->buildViolation($constraint->corruptedMessage)
193 ->setCode(Image::CORRUPTED_IMAGE_ERROR)
199 imagedestroy($resource);