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

[6.x] Fix dimension ratio calculation #34003

Merged
merged 2 commits into from Aug 25, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Expand Up @@ -558,7 +558,7 @@ protected function failsRatioCheck($parameters, $width, $height)
[1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d'))
);

$precision = 1 / max($width, $height);
$precision = 1 / (max($width, $height) + 1);

return abs($numerator / $denominator - $width / $height) > $precision;
}
Expand Down
12 changes: 10 additions & 2 deletions tests/Validation/ValidationValidatorTest.php
Expand Up @@ -2798,7 +2798,7 @@ public function testValidateImageDimensions()
$v = new Validator($trans, ['x' => $svgXmlUploadedFile], ['x' => 'dimensions:max_width=1,max_height=1']);
$this->assertTrue($v->passes());

$svgXmlFile = new File(__DIR__.'/fixtures/image.svg', '', 'image/svg+xml', null, null, true);
$svgXmlFile = new UploadedFile(__DIR__.'/fixtures/image.svg', '', 'image/svg+xml', null, null, true);
$trans = $this->getIlluminateArrayTranslator();

$v = new Validator($trans, ['x' => $svgXmlFile], ['x' => 'dimensions:max_width=1,max_height=1']);
Expand All @@ -2811,11 +2811,19 @@ public function testValidateImageDimensions()
$v = new Validator($trans, ['x' => $svgUploadedFile], ['x' => 'dimensions:max_width=1,max_height=1']);
$this->assertTrue($v->passes());

$svgFile = new File(__DIR__.'/fixtures/image2.svg', '', 'image/svg', null, null, true);
$svgFile = new UploadedFile(__DIR__.'/fixtures/image2.svg', '', 'image/svg', null, null, true);
$trans = $this->getIlluminateArrayTranslator();

$v = new Validator($trans, ['x' => $svgFile], ['x' => 'dimensions:max_width=1,max_height=1']);
$this->assertTrue($v->passes());

// Knowing that demo image4.png has width = 64 and height = 65
$uploadedFile = new UploadedFile(__DIR__.'/fixtures/image4.png', '', null, null, true);
$trans = $this->getIlluminateArrayTranslator();

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

/**
Expand Down
Binary file added tests/Validation/fixtures/image4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.