Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Backport fixes for image validation #17983

Merged
merged 4 commits into from
Feb 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.