From d5fd7b96668cd13369cb9af61887f87057d91c7e Mon Sep 17 00:00:00 2001 From: Ahmed Bally Date: Sun, 5 May 2024 15:14:20 +0300 Subject: [PATCH 1/6] fix image ration validation for high ratio images --- .../Validation/Concerns/ValidatesAttributes.php | 2 +- tests/Validation/ValidationValidatorTest.php | 8 ++++++++ tests/Validation/fixtures/image5.png | Bin 0 -> 5898 bytes 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 tests/Validation/fixtures/image5.png diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index b8fb2b1ac589..b0cd61f067a2 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -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, $height) + 1); return abs($numerator / $denominator - $width / $height) > $precision; } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 942dff373d03..65f6a66ff705 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -4922,6 +4922,14 @@ 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 image4.png has width = 64 and height = 65 + $uploadedFile = new UploadedFile(__DIR__.'/fixtures/image5.png', '', null, null, true); + $trans = $this->getIlluminateArrayTranslator(); + + // Ensure validation doesn't erroneously fail when ratio doesn't matches + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=16/9']); + $this->assertTrue($v->passes()); } public function testValidateMimetypes() diff --git a/tests/Validation/fixtures/image5.png b/tests/Validation/fixtures/image5.png new file mode 100644 index 0000000000000000000000000000000000000000..07f80a45d2e46a6ef6c98b54677db3533fa485a0 GIT binary patch literal 5898 zcmeAS@N?(olHy`uVBq!ia0y~yU=3qnU}oT81B#rtvpEE$7>k44ofy`glX(f`u%tWs zIx;Y9?C1WI$O`0h7I;J!GcfQS24TkI`72Tw7{um#x;TbZ#J#<-k+H#m=fH*w|LfV> z}lqb1>J zNjO>(j+TU@CE;jEI9d{pmV~1v;b=)XS`v{hx^Kk$G literal 0 HcmV?d00001 From fc481f93b2d9af65c9cb939e8c0c0a05f96a25b0 Mon Sep 17 00:00:00 2001 From: Ahmed Bally Date: Sun, 5 May 2024 15:24:38 +0300 Subject: [PATCH 2/6] fix style ci --- 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 b0cd61f067a2..532c075e30d5 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -761,7 +761,7 @@ protected function failsRatioCheck($parameters, $width, $height) [1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d')) ); - $precision = 1 / (max($width / $height, $height) + 1); + $precision = 1 / (max($width / $height, $height) + 1); return abs($numerator / $denominator - $width / $height) > $precision; } From a2f65e1a62d25699bede49daae49794cc3f21d85 Mon Sep 17 00:00:00 2001 From: Ahmed Bally Date: Sun, 5 May 2024 15:35:01 +0300 Subject: [PATCH 3/6] enhance comments --- tests/Validation/ValidationValidatorTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 65f6a66ff705..f3d1d3691359 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -4923,11 +4923,10 @@ public function testValidateImageDimensions() $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1']); $this->assertFalse($v->passes()); - // Knowing that demo image4.png has width = 64 and height = 65 + // Knowing that demo image5.png has width = 1366 and height = 768 $uploadedFile = new UploadedFile(__DIR__.'/fixtures/image5.png', '', null, null, true); $trans = $this->getIlluminateArrayTranslator(); - // Ensure validation doesn't erroneously fail when ratio doesn't matches $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=16/9']); $this->assertTrue($v->passes()); } From 4992548e56a456c2d83890631d5cf900bba5682c Mon Sep 17 00:00:00 2001 From: Ahmed Bally Date: Sun, 5 May 2024 17:57:17 +0300 Subject: [PATCH 4/6] enhance precision equation --- 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 532c075e30d5..29a74b66ee0b 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -761,7 +761,7 @@ protected function failsRatioCheck($parameters, $width, $height) [1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d')) ); - $precision = 1 / (max($width / $height, $height) + 1); + $precision = 1 / (max(($width / $height) * 10, $height) + 1); return abs($numerator / $denominator - $width / $height) > $precision; } From c330f49a3f670c303b21fc269ac59018726b50a8 Mon Sep 17 00:00:00 2001 From: Ahmed Bally Date: Sun, 5 May 2024 18:27:49 +0300 Subject: [PATCH 5/6] enhance precision equation --- 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 29a74b66ee0b..cbdac917bfb9 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -761,7 +761,7 @@ protected function failsRatioCheck($parameters, $width, $height) [1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d')) ); - $precision = 1 / (max(($width / $height) * 10, $height) + 1); + $precision = 1 / (max( ($width + $height) / 2, $height) + 1); return abs($numerator / $denominator - $width / $height) > $precision; } From d2bcf16ed2d301813ef35e7f86cd0b33dde9e2aa Mon Sep 17 00:00:00 2001 From: Ahmed Bally Date: Sun, 5 May 2024 18:33:46 +0300 Subject: [PATCH 6/6] enhance precision equation --- 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 cbdac917bfb9..20e760080786 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -761,7 +761,7 @@ protected function failsRatioCheck($parameters, $width, $height) [1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d')) ); - $precision = 1 / (max( ($width + $height) / 2, $height) + 1); + $precision = 1 / (max(($width + $height) / 2, $height) + 1); return abs($numerator / $denominator - $width / $height) > $precision; }