/
ImageValidator.php
113 lines (91 loc) · 3.63 KB
/
ImageValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
/**
* Validates whether a value is a valid image file and is valid
* against minWidth, maxWidth, minHeight and maxHeight constraints.
*
* @author Benjamin Dulau <benjamin.dulau@gmail.com>
*/
class ImageValidator extends FileValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
$violations = count($this->context->getViolations());
parent::validate($value, $constraint);
$failed = count($this->context->getViolations()) !== $violations;
if ($failed || null === $value || '' === $value) {
return;
}
if (null === $constraint->minWidth && null === $constraint->maxWidth
&& null === $constraint->minHeight && null === $constraint->maxHeight) {
return;
}
$size = @getimagesize($value);
if (empty($size) || ($size[0] === 0) || ($size[1] === 0)) {
$this->context->addViolation($constraint->sizeNotDetectedMessage);
return;
}
$width = $size[0];
$height = $size[1];
if ($constraint->minWidth) {
if (!ctype_digit((string) $constraint->minWidth)) {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum width', $constraint->minWidth));
}
if ($width < $constraint->minWidth) {
$this->context->addViolation($constraint->minWidthMessage, array(
'{{ width }}' => $width,
'{{ min_width }}' => $constraint->minWidth,
));
return;
}
}
if ($constraint->maxWidth) {
if (!ctype_digit((string) $constraint->maxWidth)) {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum width', $constraint->maxWidth));
}
if ($width > $constraint->maxWidth) {
$this->context->addViolation($constraint->maxWidthMessage, array(
'{{ width }}' => $width,
'{{ max_width }}' => $constraint->maxWidth,
));
return;
}
}
if ($constraint->minHeight) {
if (!ctype_digit((string) $constraint->minHeight)) {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid minimum height', $constraint->minHeight));
}
if ($height < $constraint->minHeight) {
$this->context->addViolation($constraint->minHeightMessage, array(
'{{ height }}' => $height,
'{{ min_height }}' => $constraint->minHeight,
));
return;
}
}
if ($constraint->maxHeight) {
if (!ctype_digit((string) $constraint->maxHeight)) {
throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum height', $constraint->maxHeight));
}
if ($height > $constraint->maxHeight) {
$this->context->addViolation($constraint->maxHeightMessage, array(
'{{ height }}' => $height,
'{{ max_height }}' => $constraint->maxHeight,
));
}
}
}
}