Skip to content

Commit

Permalink
[11.x] Fix ratio validation for high ratio images (#51296)
Browse files Browse the repository at this point in the history
* fix image ration validation for high ratio images

* fix style ci

* enhance comments

* enhance precision equation

* enhance precision equation

* enhance precision equation
  • Loading branch information
ahmedbally authored May 5, 2024
1 parent 2dee1de commit a8829f7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ protected function failsRatioCheck($parameters, $width, $height)
[1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d'))
);

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

return abs($numerator / $denominator - $width / $height) > $precision;
}
Expand Down
7 changes: 7 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4922,6 +4922,13 @@ public function testValidateImageDimensions()
// 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());

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

$v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=16/9']);
$this->assertTrue($v->passes());
}

public function testValidateMimetypes()
Expand Down
Binary file added tests/Validation/fixtures/image5.png
Loading
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 a8829f7

Please sign in to comment.