Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 114 lines (91 sloc) 3.72 kB
a6145bb @fabpot fixed CS
fabpot authored
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
17 /**
18 * Validates whether a value is a valid image file and is valid
19 * against minWidth, maxWidth, minHeight and maxHeight constraints
20 *
21 * @author Benjamin Dulau <benjamin.dulau@gmail.com>
22 */
23 class ImageValidator extends FileValidator
24 {
9bde527 @danielholmes [Validator] Added inheritDoc phpdoc for validate methods
danielholmes authored
25 /**
26 * {@inheritDoc}
27 */
734609c @webmozart [Validator] Renamed ConstraintValidatorInterface::isValid() to valida…
webmozart authored
28 public function validate($value, Constraint $constraint)
a6145bb @fabpot fixed CS
fabpot authored
29 {
529a58d @webmozart [Validator] Removed return value from ConstraintValidatorInterface::i…
webmozart authored
30 $violations = count($this->context->getViolations());
31
734609c @webmozart [Validator] Renamed ConstraintValidatorInterface::isValid() to valida…
webmozart authored
32 parent::validate($value, $constraint);
529a58d @webmozart [Validator] Removed return value from ConstraintValidatorInterface::i…
webmozart authored
33
734609c @webmozart [Validator] Renamed ConstraintValidatorInterface::isValid() to valida…
webmozart authored
34 $failed = count($this->context->getViolations()) !== $violations;
a6145bb @fabpot fixed CS
fabpot authored
35
734609c @webmozart [Validator] Renamed ConstraintValidatorInterface::isValid() to valida…
webmozart authored
36 if ($failed || null === $value || '' === $value) {
529a58d @webmozart [Validator] Removed return value from ConstraintValidatorInterface::i…
webmozart authored
37 return;
a6145bb @fabpot fixed CS
fabpot authored
38 }
39
40 if (null === $constraint->minWidth && null === $constraint->maxWidth
41 && null === $constraint->minHeight && null === $constraint->maxHeight) {
529a58d @webmozart [Validator] Removed return value from ConstraintValidatorInterface::i…
webmozart authored
42 return;
a6145bb @fabpot fixed CS
fabpot authored
43 }
44
45 $size = @getimagesize($value);
46 if (empty($size) || ($size[0] === 0) || ($size[1] === 0)) {
d7cb249 @webmozart [Validator] Deprecated ConstraintValidator methods setMessage(), getM…
webmozart authored
47 $this->context->addViolation($constraint->sizeNotDetectedMessage);
a6145bb @fabpot fixed CS
fabpot authored
48
529a58d @webmozart [Validator] Removed return value from ConstraintValidatorInterface::i…
webmozart authored
49 return;
a6145bb @fabpot fixed CS
fabpot authored
50 }
51
52 $width = $size[0];
53 $height = $size[1];
54
55 if ($constraint->minWidth) {
56 if (!ctype_digit((string) $constraint->minWidth)) {
57 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum width', $constraint->minWidth));
58 }
59
60 if ($width < $constraint->minWidth) {
d7cb249 @webmozart [Validator] Deprecated ConstraintValidator methods setMessage(), getM…
webmozart authored
61 $this->context->addViolation($constraint->minWidthMessage, array(
a6145bb @fabpot fixed CS
fabpot authored
62 '{{ width }}' => $width,
63 '{{ min_width }}' => $constraint->minWidth
64 ));
65
529a58d @webmozart [Validator] Removed return value from ConstraintValidatorInterface::i…
webmozart authored
66 return;
a6145bb @fabpot fixed CS
fabpot authored
67 }
68 }
69
70 if ($constraint->maxWidth) {
71 if (!ctype_digit((string) $constraint->maxWidth)) {
72 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum width', $constraint->maxWidth));
73 }
74
75 if ($width > $constraint->maxWidth) {
d7cb249 @webmozart [Validator] Deprecated ConstraintValidator methods setMessage(), getM…
webmozart authored
76 $this->context->addViolation($constraint->maxWidthMessage, array(
a6145bb @fabpot fixed CS
fabpot authored
77 '{{ width }}' => $width,
78 '{{ max_width }}' => $constraint->maxWidth
79 ));
80
529a58d @webmozart [Validator] Removed return value from ConstraintValidatorInterface::i…
webmozart authored
81 return;
a6145bb @fabpot fixed CS
fabpot authored
82 }
83 }
84
85 if ($constraint->minHeight) {
86 if (!ctype_digit((string) $constraint->minHeight)) {
87 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum height', $constraint->minHeight));
88 }
89
90 if ($height < $constraint->minHeight) {
d7cb249 @webmozart [Validator] Deprecated ConstraintValidator methods setMessage(), getM…
webmozart authored
91 $this->context->addViolation($constraint->minHeightMessage, array(
a6145bb @fabpot fixed CS
fabpot authored
92 '{{ height }}' => $height,
93 '{{ min_height }}' => $constraint->minHeight
94 ));
95
529a58d @webmozart [Validator] Removed return value from ConstraintValidatorInterface::i…
webmozart authored
96 return;
a6145bb @fabpot fixed CS
fabpot authored
97 }
98 }
99
100 if ($constraint->maxHeight) {
101 if (!ctype_digit((string) $constraint->maxHeight)) {
102 throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum height', $constraint->maxHeight));
103 }
104
105 if ($height > $constraint->maxHeight) {
d7cb249 @webmozart [Validator] Deprecated ConstraintValidator methods setMessage(), getM…
webmozart authored
106 $this->context->addViolation($constraint->maxHeightMessage, array(
a6145bb @fabpot fixed CS
fabpot authored
107 '{{ height }}' => $height,
108 '{{ max_height }}' => $constraint->maxHeight
109 ));
110 }
111 }
112 }
113 }
Something went wrong with that request. Please try again.