Skip to content

Commit a8829f7

Browse files
authored
[11.x] Fix ratio validation for high ratio images (#51296)
* fix image ration validation for high ratio images * fix style ci * enhance comments * enhance precision equation * enhance precision equation * enhance precision equation
1 parent 2dee1de commit a8829f7

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ protected function failsRatioCheck($parameters, $width, $height)
761761
[1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d'))
762762
);
763763

764-
$precision = 1 / (max($width, $height) + 1);
764+
$precision = 1 / (max(($width + $height) / 2, $height) + 1);
765765

766766
return abs($numerator / $denominator - $width / $height) > $precision;
767767
}

tests/Validation/ValidationValidatorTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -4922,6 +4922,13 @@ public function testValidateImageDimensions()
49224922
// Ensure validation doesn't erroneously fail when ratio doesn't matches
49234923
$v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1']);
49244924
$this->assertFalse($v->passes());
4925+
4926+
// Knowing that demo image5.png has width = 1366 and height = 768
4927+
$uploadedFile = new UploadedFile(__DIR__.'/fixtures/image5.png', '', null, null, true);
4928+
$trans = $this->getIlluminateArrayTranslator();
4929+
4930+
$v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=16/9']);
4931+
$this->assertTrue($v->passes());
49254932
}
49264933

49274934
public function testValidateMimetypes()

tests/Validation/fixtures/image5.png

5.76 KB
Loading

0 commit comments

Comments
 (0)