diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 4de6c3890eba..b39602c2adff 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -478,7 +478,9 @@ protected function failsRatioCheck($parameters, $width, $height) [1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d')) ); - return abs($numerator / $denominator - $width / $height) > 0.000001; + $precision = 1 / max($width, $height); + + return abs($numerator / $denominator - $width / $height) > $precision; } /** diff --git a/tests/Validation/ValidationDimensionsRuleTest.php b/tests/Validation/ValidationDimensionsRuleTest.php index 63881854eb52..6f5ee5763a07 100644 --- a/tests/Validation/ValidationDimensionsRuleTest.php +++ b/tests/Validation/ValidationDimensionsRuleTest.php @@ -21,5 +21,9 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule() $rule = Rule::dimensions()->maxWidth(1000)->maxHeight(500)->ratio(3 / 2); $this->assertEquals('dimensions:max_width=1000,max_height=500,ratio=1.5', (string) $rule); + + $rule = new Dimensions(['ratio' => '2/3']); + + $this->assertEquals('dimensions:ratio=2/3', (string) $rule); } } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 8dedc31b7bd7..c4652f68f1fe 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -1972,6 +1972,9 @@ public function testValidateImage() $this->assertTrue($v->passes()); } + /** + * @group dimension + */ public function testValidateImageDimensions() { // Knowing that demo image.png has width = 3 and height = 2 @@ -2037,6 +2040,14 @@ public function testValidateImageDimensions() $v = new Validator($trans, ['x' => $emptyUploadedFile], ['x' => 'dimensions:min_width=1']); $this->assertTrue($v->fails()); + + // Knowing that demo image3.png has width = 7 and height = 10 + $uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/image3.png', '', null, null, null, true); + $trans = $this->getIlluminateArrayTranslator(); + + // Ensure validation doesn't erroneously fail when ratio has no fractional part + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=2/3']); + $this->assertTrue($v->passes()); } /** diff --git a/tests/Validation/fixtures/image3.png b/tests/Validation/fixtures/image3.png new file mode 100644 index 000000000000..cc580eb06bfe Binary files /dev/null and b/tests/Validation/fixtures/image3.png differ