Skip to content

Commit

Permalink
[6.x] Fix dimension ratio calculation (#34003)
Browse files Browse the repository at this point in the history
* Fix dimension ratio calculation

* Update comment
  • Loading branch information
driesvints committed Aug 25, 2020
1 parent c8d453c commit bf97fb0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
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.

0 comments on commit bf97fb0

Please sign in to comment.