Skip to content

Commit

Permalink
[5.3] Backport fixes for image validation (#17983)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
vlakoff authored and taylorotwell committed Feb 18, 2017
1 parent 3d98ed7 commit 666a752
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,7 @@ protected function validateImage($attribute, $value)
*/
protected function validateDimensions($attribute, $value, $parameters)
{
if (! $this->isAValidFileInstance($value) || ! $sizeDetails = getimagesize($value->getRealPath())) {
if (! $this->isAValidFileInstance($value) || ! $sizeDetails = @getimagesize($value->getRealPath())) {
return false;
}

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

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

return true;
Expand Down
19 changes: 17 additions & 2 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1855,8 +1855,8 @@ public function testValidateImage()

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

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

$v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1']);
$this->assertTrue($v->fails());

// This test fails without suppressing warnings on getimagesize() due to a read error.
$emptyUploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/empty.png', '', null, null, null, true);
$trans = $this->getIlluminateArrayTranslator();

$v = new Validator($trans, ['x' => $emptyUploadedFile], ['x' => 'dimensions:min_width=1']);
$this->assertTrue($v->fails());

// Knowing that demo image2.png has width = 4 and height = 2
$uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/image2.png', '', null, null, null, true);
$trans = $this->getIlluminateArrayTranslator();

// Ensure validation doesn't erroneously fail when ratio has no fractional part
$v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=2/1']);
$this->assertTrue($v->passes());
}

/**
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed tests/Validation/fixtures/image.gif
Binary file not shown.
Binary file added tests/Validation/fixtures/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/Validation/fixtures/image2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 666a752

Please sign in to comment.