AbstractComparison.php
1 year ago
AbstractComparisonValidator.php
1 year ago
All.php
1 year ago
AllValidator.php
4 years ago
AtLeastOneOf.php
1 year ago
AtLeastOneOfValidator.php
1 year ago
Bic.php
1 year ago
BicValidator.php
1 year ago
Blank.php
1 year ago
BlankValidator.php
4 years ago
Callback.php
1 year ago
CallbackValidator.php
4 years ago
CardScheme.php
1 year ago
CardSchemeValidator.php
1 year ago
Cascade.php
1 year ago
Choice.php
1 year ago
ChoiceValidator.php
1 year ago
Cidr.php
1 year ago
CidrValidator.php
4 years ago
Collection.php
1 year ago
CollectionValidator.php
4 years ago
Composite.php
1 year ago
Compound.php
4 years ago
CompoundValidator.php
4 years ago
Count.php
1 year ago
CountValidator.php
4 years ago
Country.php
1 year ago
CountryValidator.php
4 years ago
CssColor.php
1 year ago
CssColorValidator.php
1 year ago
Currency.php
1 year ago
CurrencyValidator.php
4 years ago
Date.php
1 year ago
DateTime.php
1 year ago
DateTimeValidator.php
2 years ago
DateValidator.php
1 year ago
DisableAutoMapping.php
1 year ago
DivisibleBy.php
4 years ago
DivisibleByValidator.php
4 years ago
Email.php
1 year ago
EmailValidator.php
1 year ago
EnableAutoMapping.php
1 year ago
EqualTo.php
4 years ago
EqualToValidator.php
4 years ago
Existence.php
4 years ago
Expression.php
1 year ago
ExpressionLanguageSyntax.php
1 year ago
ExpressionLanguageSyntaxValidator.php
1 year ago
ExpressionValidator.php
1 year ago
File.php
1 year ago
FileValidator.php
4 years ago
GreaterThan.php
4 years ago
GreaterThanOrEqual.php
4 years ago
GreaterThanOrEqualValidator.php
4 years ago
GreaterThanValidator.php
4 years ago
GroupSequence.php
4 years ago
GroupSequenceProvider.php
4 years ago
Hostname.php
1 year ago
HostnameValidator.php
4 years ago
Iban.php
1 year ago
IbanValidator.php
1 year ago
IdenticalTo.php
4 years ago
IdenticalToValidator.php
4 years ago
Image.php
1 year ago
ImageValidator.php
4 years ago
Ip.php
1 year ago
IpValidator.php
4 years ago
IsFalse.php
1 year ago
IsFalseValidator.php
4 years ago
IsNull.php
1 year ago
IsNullValidator.php
4 years ago
IsTrue.php
1 year ago
IsTrueValidator.php
4 years ago
Isbn.php
1 year ago
IsbnValidator.php
1 year ago
Isin.php
1 year ago
IsinValidator.php
4 years ago
Issn.php
1 year ago
IssnValidator.php
4 years ago
Json.php
1 year ago
JsonValidator.php
1 year ago
Language.php
1 year ago
LanguageValidator.php
4 years ago
Length.php
1 year ago
LengthValidator.php
4 years ago
LessThan.php
4 years ago
LessThanOrEqual.php
4 years ago
LessThanOrEqualValidator.php
4 years ago
LessThanValidator.php
4 years ago
Locale.php
1 year ago
LocaleValidator.php
4 years ago
Luhn.php
1 year ago
LuhnValidator.php
4 years ago
Negative.php
4 years ago
NegativeOrZero.php
4 years ago
NotBlank.php
1 year ago
NotBlankValidator.php
4 years ago
NotCompromisedPassword.php
1 year ago
NotCompromisedPasswordValidator.php
1 year ago
NotEqualTo.php
4 years ago
NotEqualToValidator.php
4 years ago
NotIdenticalTo.php
4 years ago
NotIdenticalToValidator.php
4 years ago
NotNull.php
1 year ago
NotNullValidator.php
4 years ago
NumberConstraintTrait.php
4 years ago
Optional.php
4 years ago
Positive.php
4 years ago
PositiveOrZero.php
4 years ago
Range.php
1 year ago
RangeValidator.php
1 year ago
Regex.php
1 year ago
RegexValidator.php
4 years ago
Required.php
4 years ago
Sequentially.php
1 year ago
SequentiallyValidator.php
4 years ago
Time.php
1 year ago
TimeValidator.php
1 year ago
Timezone.php
1 year ago
TimezoneValidator.php
1 year ago
Traverse.php
4 years ago
Type.php
1 year ago
TypeValidator.php
4 years ago
Ulid.php
1 year ago
UlidValidator.php
2 years ago
Unique.php
1 year ago
UniqueValidator.php
1 year ago
Url.php
1 year ago
UrlValidator.php
1 year ago
Uuid.php
1 year ago
UuidValidator.php
4 years ago
Valid.php
1 year ago
ValidValidator.php
4 years ago
ZeroComparisonConstraintTrait.php
1 year ago
index.php
3 years ago
ImageValidator.php
122 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Validator\Constraints; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\Validator\Constraint; |
| 5 | use MailPoetVendor\Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
| 6 | use MailPoetVendor\Symfony\Component\Validator\Exception\LogicException; |
| 7 | use MailPoetVendor\Symfony\Component\Validator\Exception\UnexpectedTypeException; |
| 8 | class ImageValidator extends FileValidator |
| 9 | { |
| 10 | public function validate($value, Constraint $constraint) |
| 11 | { |
| 12 | if (!$constraint instanceof Image) { |
| 13 | throw new UnexpectedTypeException($constraint, Image::class); |
| 14 | } |
| 15 | $violations = \count($this->context->getViolations()); |
| 16 | parent::validate($value, $constraint); |
| 17 | $failed = \count($this->context->getViolations()) !== $violations; |
| 18 | if ($failed || null === $value || '' === $value) { |
| 19 | return; |
| 20 | } |
| 21 | if (null === $constraint->minWidth && null === $constraint->maxWidth && null === $constraint->minHeight && null === $constraint->maxHeight && null === $constraint->minPixels && null === $constraint->maxPixels && null === $constraint->minRatio && null === $constraint->maxRatio && $constraint->allowSquare && $constraint->allowLandscape && $constraint->allowPortrait && !$constraint->detectCorrupted) { |
| 22 | return; |
| 23 | } |
| 24 | $size = @\getimagesize($value); |
| 25 | if (empty($size) || 0 === $size[0] || 0 === $size[1]) { |
| 26 | $this->context->buildViolation($constraint->sizeNotDetectedMessage)->setCode(Image::SIZE_NOT_DETECTED_ERROR)->addViolation(); |
| 27 | return; |
| 28 | } |
| 29 | $width = $size[0]; |
| 30 | $height = $size[1]; |
| 31 | if ($constraint->minWidth) { |
| 32 | if (!\ctype_digit((string) $constraint->minWidth)) { |
| 33 | throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid minimum width.', $constraint->minWidth)); |
| 34 | } |
| 35 | if ($width < $constraint->minWidth) { |
| 36 | $this->context->buildViolation($constraint->minWidthMessage)->setParameter('{{ width }}', $width)->setParameter('{{ min_width }}', $constraint->minWidth)->setCode(Image::TOO_NARROW_ERROR)->addViolation(); |
| 37 | return; |
| 38 | } |
| 39 | } |
| 40 | if ($constraint->maxWidth) { |
| 41 | if (!\ctype_digit((string) $constraint->maxWidth)) { |
| 42 | throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid maximum width.', $constraint->maxWidth)); |
| 43 | } |
| 44 | if ($width > $constraint->maxWidth) { |
| 45 | $this->context->buildViolation($constraint->maxWidthMessage)->setParameter('{{ width }}', $width)->setParameter('{{ max_width }}', $constraint->maxWidth)->setCode(Image::TOO_WIDE_ERROR)->addViolation(); |
| 46 | return; |
| 47 | } |
| 48 | } |
| 49 | if ($constraint->minHeight) { |
| 50 | if (!\ctype_digit((string) $constraint->minHeight)) { |
| 51 | throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid minimum height.', $constraint->minHeight)); |
| 52 | } |
| 53 | if ($height < $constraint->minHeight) { |
| 54 | $this->context->buildViolation($constraint->minHeightMessage)->setParameter('{{ height }}', $height)->setParameter('{{ min_height }}', $constraint->minHeight)->setCode(Image::TOO_LOW_ERROR)->addViolation(); |
| 55 | return; |
| 56 | } |
| 57 | } |
| 58 | if ($constraint->maxHeight) { |
| 59 | if (!\ctype_digit((string) $constraint->maxHeight)) { |
| 60 | throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid maximum height.', $constraint->maxHeight)); |
| 61 | } |
| 62 | if ($height > $constraint->maxHeight) { |
| 63 | $this->context->buildViolation($constraint->maxHeightMessage)->setParameter('{{ height }}', $height)->setParameter('{{ max_height }}', $constraint->maxHeight)->setCode(Image::TOO_HIGH_ERROR)->addViolation(); |
| 64 | } |
| 65 | } |
| 66 | $pixels = $width * $height; |
| 67 | if (null !== $constraint->minPixels) { |
| 68 | if (!\ctype_digit((string) $constraint->minPixels)) { |
| 69 | throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid minimum amount of pixels.', $constraint->minPixels)); |
| 70 | } |
| 71 | if ($pixels < $constraint->minPixels) { |
| 72 | $this->context->buildViolation($constraint->minPixelsMessage)->setParameter('{{ pixels }}', $pixels)->setParameter('{{ min_pixels }}', $constraint->minPixels)->setParameter('{{ height }}', $height)->setParameter('{{ width }}', $width)->setCode(Image::TOO_FEW_PIXEL_ERROR)->addViolation(); |
| 73 | } |
| 74 | } |
| 75 | if (null !== $constraint->maxPixels) { |
| 76 | if (!\ctype_digit((string) $constraint->maxPixels)) { |
| 77 | throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid maximum amount of pixels.', $constraint->maxPixels)); |
| 78 | } |
| 79 | if ($pixels > $constraint->maxPixels) { |
| 80 | $this->context->buildViolation($constraint->maxPixelsMessage)->setParameter('{{ pixels }}', $pixels)->setParameter('{{ max_pixels }}', $constraint->maxPixels)->setParameter('{{ height }}', $height)->setParameter('{{ width }}', $width)->setCode(Image::TOO_MANY_PIXEL_ERROR)->addViolation(); |
| 81 | } |
| 82 | } |
| 83 | $ratio = \round($width / $height, 2); |
| 84 | if (null !== $constraint->minRatio) { |
| 85 | if (!\is_numeric((string) $constraint->minRatio)) { |
| 86 | throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid minimum ratio.', $constraint->minRatio)); |
| 87 | } |
| 88 | if ($ratio < \round($constraint->minRatio, 2)) { |
| 89 | $this->context->buildViolation($constraint->minRatioMessage)->setParameter('{{ ratio }}', $ratio)->setParameter('{{ min_ratio }}', \round($constraint->minRatio, 2))->setCode(Image::RATIO_TOO_SMALL_ERROR)->addViolation(); |
| 90 | } |
| 91 | } |
| 92 | if (null !== $constraint->maxRatio) { |
| 93 | if (!\is_numeric((string) $constraint->maxRatio)) { |
| 94 | throw new ConstraintDefinitionException(\sprintf('"%s" is not a valid maximum ratio.', $constraint->maxRatio)); |
| 95 | } |
| 96 | if ($ratio > \round($constraint->maxRatio, 2)) { |
| 97 | $this->context->buildViolation($constraint->maxRatioMessage)->setParameter('{{ ratio }}', $ratio)->setParameter('{{ max_ratio }}', \round($constraint->maxRatio, 2))->setCode(Image::RATIO_TOO_BIG_ERROR)->addViolation(); |
| 98 | } |
| 99 | } |
| 100 | if (!$constraint->allowSquare && $width == $height) { |
| 101 | $this->context->buildViolation($constraint->allowSquareMessage)->setParameter('{{ width }}', $width)->setParameter('{{ height }}', $height)->setCode(Image::SQUARE_NOT_ALLOWED_ERROR)->addViolation(); |
| 102 | } |
| 103 | if (!$constraint->allowLandscape && $width > $height) { |
| 104 | $this->context->buildViolation($constraint->allowLandscapeMessage)->setParameter('{{ width }}', $width)->setParameter('{{ height }}', $height)->setCode(Image::LANDSCAPE_NOT_ALLOWED_ERROR)->addViolation(); |
| 105 | } |
| 106 | if (!$constraint->allowPortrait && $width < $height) { |
| 107 | $this->context->buildViolation($constraint->allowPortraitMessage)->setParameter('{{ width }}', $width)->setParameter('{{ height }}', $height)->setCode(Image::PORTRAIT_NOT_ALLOWED_ERROR)->addViolation(); |
| 108 | } |
| 109 | if ($constraint->detectCorrupted) { |
| 110 | if (!\function_exists('imagecreatefromstring')) { |
| 111 | throw new LogicException('Corrupted images detection requires installed and enabled GD extension.'); |
| 112 | } |
| 113 | $resource = @\imagecreatefromstring(\file_get_contents($value)); |
| 114 | if (\false === $resource) { |
| 115 | $this->context->buildViolation($constraint->corruptedMessage)->setCode(Image::CORRUPTED_IMAGE_ERROR)->addViolation(); |
| 116 | return; |
| 117 | } |
| 118 | \imagedestroy($resource); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 |