Skip to content

Commit 666a752

Browse files
vlakofftaylorotwell
authored andcommitted
[5.3] Backport fixes for image validation (#17983)
* Suppress getimagesize() warnings Backport of #17944. `getimagesize` will fail with a notice on files smaller than 12 bytes. This ends up becoming an ErrorException which is pretty annoying. Suppressing the warning is enough to still end up with a `false` result which will fail the validation gracefully instead of grueing up your stew. * Proper float comparison in ratio validation * Convert fixtures to PNG format * Add test for validation of ratio with no fractional part
1 parent 3d98ed7 commit 666a752

File tree

6 files changed

+19
-4
lines changed

6 files changed

+19
-4
lines changed

src/Illuminate/Validation/Validator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,7 @@ protected function validateImage($attribute, $value)
16611661
*/
16621662
protected function validateDimensions($attribute, $value, $parameters)
16631663
{
1664-
if (! $this->isAValidFileInstance($value) || ! $sizeDetails = getimagesize($value->getRealPath())) {
1664+
if (! $this->isAValidFileInstance($value) || ! $sizeDetails = @getimagesize($value->getRealPath())) {
16651665
return false;
16661666
}
16671667

@@ -1687,7 +1687,7 @@ protected function validateDimensions($attribute, $value, $parameters)
16871687
[1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d'))
16881688
);
16891689

1690-
return $numerator / $denominator == $width / $height;
1690+
return abs($numerator / $denominator - $width / $height) < 0.000001;
16911691
}
16921692

16931693
return true;

tests/Validation/ValidationValidatorTest.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -1855,8 +1855,8 @@ public function testValidateImage()
18551855

18561856
public function testValidateImageDimensions()
18571857
{
1858-
// Knowing that demo image.gif has width = 3 and height = 2
1859-
$uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/image.gif', '', null, null, null, true);
1858+
// Knowing that demo image.png has width = 3 and height = 2
1859+
$uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/image.png', '', null, null, null, true);
18601860
$trans = $this->getIlluminateArrayTranslator();
18611861

18621862
$v = new Validator($trans, ['x' => 'file'], ['x' => 'dimensions']);
@@ -1903,6 +1903,21 @@ public function testValidateImageDimensions()
19031903

19041904
$v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1']);
19051905
$this->assertTrue($v->fails());
1906+
1907+
// This test fails without suppressing warnings on getimagesize() due to a read error.
1908+
$emptyUploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/empty.png', '', null, null, null, true);
1909+
$trans = $this->getIlluminateArrayTranslator();
1910+
1911+
$v = new Validator($trans, ['x' => $emptyUploadedFile], ['x' => 'dimensions:min_width=1']);
1912+
$this->assertTrue($v->fails());
1913+
1914+
// Knowing that demo image2.png has width = 4 and height = 2
1915+
$uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/image2.png', '', null, null, null, true);
1916+
$trans = $this->getIlluminateArrayTranslator();
1917+
1918+
// Ensure validation doesn't erroneously fail when ratio has no fractional part
1919+
$v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=2/1']);
1920+
$this->assertTrue($v->passes());
19061921
}
19071922

19081923
/**

tests/Validation/fixtures/empty.png

Loading

tests/Validation/fixtures/image.gif

-1.07 KB
Binary file not shown.

tests/Validation/fixtures/image.png

91 Bytes
Loading

tests/Validation/fixtures/image2.png

91 Bytes
Loading

0 commit comments

Comments
 (0)