Skip to content

Commit bf97fb0

Browse files
authored
[6.x] Fix dimension ratio calculation (#34003)
* Fix dimension ratio calculation * Update comment
1 parent c8d453c commit bf97fb0

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ protected function failsRatioCheck($parameters, $width, $height)
558558
[1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d'))
559559
);
560560

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

563563
return abs($numerator / $denominator - $width / $height) > $precision;
564564
}

tests/Validation/ValidationValidatorTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2798,7 +2798,7 @@ public function testValidateImageDimensions()
27982798
$v = new Validator($trans, ['x' => $svgXmlUploadedFile], ['x' => 'dimensions:max_width=1,max_height=1']);
27992799
$this->assertTrue($v->passes());
28002800

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

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

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

28172817
$v = new Validator($trans, ['x' => $svgFile], ['x' => 'dimensions:max_width=1,max_height=1']);
28182818
$this->assertTrue($v->passes());
2819+
2820+
// Knowing that demo image4.png has width = 64 and height = 65
2821+
$uploadedFile = new UploadedFile(__DIR__.'/fixtures/image4.png', '', null, null, true);
2822+
$trans = $this->getIlluminateArrayTranslator();
2823+
2824+
// Ensure validation doesn't erroneously fail when ratio doesn't matches
2825+
$v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1']);
2826+
$this->assertFalse($v->passes());
28192827
}
28202828

28212829
/**

tests/Validation/fixtures/image4.png

1.73 KB
Loading

0 commit comments

Comments
 (0)