From daaa62849ea7c025de1edff2bbccbe89d0bec801 Mon Sep 17 00:00:00 2001 From: Charlie Kassel Date: Fri, 9 Jun 2017 14:47:21 +0100 Subject: [PATCH 1/4] allow ratio precision to be based on size of image --- src/Illuminate/Validation/Concerns/ValidatesAttributes.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 4de6c3890eba..581bbdf3136c 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -478,7 +478,10 @@ protected function failsRatioCheck($parameters, $width, $height) [1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d')) ); - return abs($numerator / $denominator - $width / $height) > 0.000001; + $maxDimension = max($width, $height); + $precision = $maxDimension / pow($maxDimension, 2); + + return abs($numerator / $denominator - $width / $height) > $precision; } /** From cf7a09d15056b37d7ddae246c5febf148cb58c50 Mon Sep 17 00:00:00 2001 From: Charlie Kassel Date: Fri, 9 Jun 2017 15:18:44 +0100 Subject: [PATCH 2/4] use exponentiation operator in preference to pow() --- src/Illuminate/Validation/Concerns/ValidatesAttributes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 581bbdf3136c..7b712ba1cacd 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -479,7 +479,7 @@ protected function failsRatioCheck($parameters, $width, $height) ); $maxDimension = max($width, $height); - $precision = $maxDimension / pow($maxDimension, 2); + $precision = $maxDimension / $maxDimension ** 2; return abs($numerator / $denominator - $width / $height) > $precision; } From d3b784d3eb00059d7ff68c3d04a7966102dfc4e8 Mon Sep 17 00:00:00 2001 From: Charlie Kassel Date: Sun, 11 Jun 2017 16:37:56 +0100 Subject: [PATCH 3/4] simplify math --- src/Illuminate/Validation/Concerns/ValidatesAttributes.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 7b712ba1cacd..b39602c2adff 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -478,8 +478,7 @@ protected function failsRatioCheck($parameters, $width, $height) [1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d')) ); - $maxDimension = max($width, $height); - $precision = $maxDimension / $maxDimension ** 2; + $precision = 1 / max($width, $height); return abs($numerator / $denominator - $width / $height) > $precision; } From 6913e8b35a737490ce11d51aca7fd18594bd58b7 Mon Sep 17 00:00:00 2001 From: Charlie Kassel Date: Mon, 12 Jun 2017 10:08:39 +0100 Subject: [PATCH 4/4] add fixture to test 2/3 ratio image --- tests/Validation/ValidationDimensionsRuleTest.php | 4 ++++ tests/Validation/ValidationValidatorTest.php | 11 +++++++++++ tests/Validation/fixtures/image3.png | Bin 0 -> 82 bytes 3 files changed, 15 insertions(+) create mode 100644 tests/Validation/fixtures/image3.png 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 0000000000000000000000000000000000000000..cc580eb06bfe6083271cf11805dd22cd59de27ac GIT binary patch literal 82 zcmeAS@N?(olHy`uVBq!ia0vp^>_E)L$P6T%n6CK&Ddqs55ZC|z|1UoxeF7xs>Eal| daXt9}69aVH^44$rjF6*2UngFVD6O#Y{ literal 0 HcmV?d00001