Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c4a0e11

Browse files
committed
Add strict image validation
1 parent caae21c commit c4a0e11

File tree

4 files changed

+121
-1
lines changed

4 files changed

+121
-1
lines changed

src/Symfony/Component/Validator/Constraints/Image.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class Image extends File
6161
public $allowSquare = true;
6262
public $allowLandscape = true;
6363
public $allowPortrait = true;
64+
public $strict = false;
6465

6566
// The constant for a wrong MIME type is taken from the parent class.
6667
public $mimeTypesMessage = 'This file is not a valid image.';

src/Symfony/Component/Validator/Constraints/ImageValidator.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ public function validate($value, Constraint $constraint)
4343
return;
4444
}
4545

46+
if ($constraint->strict) {
47+
if (function_exists('imagecreatefromstring')) {
48+
$resource = @imagecreatefromstring(file_get_contents($value));
49+
50+
if (false === $resource) {
51+
$this->context->buildViolation($constraint->mimeTypesMessage)
52+
->setCode(Image::INVALID_MIME_TYPE_ERROR)
53+
->addViolation();
54+
55+
return;
56+
}
57+
}
58+
}
59+
4660
if (null === $constraint->minWidth && null === $constraint->maxWidth
4761
&& null === $constraint->minHeight && null === $constraint->maxHeight
4862
&& null === $constraint->minRatio && null === $constraint->maxRatio

0 commit comments

Comments
 (0)