From 69d662c11d6e47fa41ea8570ad734cd78d8bff14 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Sat, 18 Feb 2017 01:05:57 +0100 Subject: [PATCH 1/4] Suppress getimagesize() warnings Backport of #17944. `getimagesize` will fail with a notice on files smaller than 12 bytes. This ends up becoming an ErrorException which is pretty annoying. Suppressing the warning is enough to still end up with a `false` result which will fail the validation gracefully instead of grueing up your stew. --- src/Illuminate/Validation/Validator.php | 2 +- tests/Validation/ValidationValidatorTest.php | 7 +++++++ tests/Validation/fixtures/empty.gif | 0 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 tests/Validation/fixtures/empty.gif diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index 680a856a5be7..e2f055ab6f52 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -1661,7 +1661,7 @@ protected function validateImage($attribute, $value) */ protected function validateDimensions($attribute, $value, $parameters) { - if (! $this->isAValidFileInstance($value) || ! $sizeDetails = getimagesize($value->getRealPath())) { + if (! $this->isAValidFileInstance($value) || ! $sizeDetails = @getimagesize($value->getRealPath())) { return false; } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 24871e6b2df2..560b76fac097 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -1903,6 +1903,13 @@ public function testValidateImageDimensions() $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1']); $this->assertTrue($v->fails()); + + // This test fails without suppressing warnings on getimagesize() due to a read error. + $emptyUploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/empty.gif', '', null, null, null, true); + $trans = $this->getIlluminateArrayTranslator(); + + $v = new Validator($trans, ['x' => $emptyUploadedFile], ['x' => 'dimensions:min_width=1']); + $this->assertTrue($v->fails()); } /** diff --git a/tests/Validation/fixtures/empty.gif b/tests/Validation/fixtures/empty.gif new file mode 100644 index 000000000000..e69de29bb2d1 From a028a4bc577fb246e4f538f1cf8202510145c592 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Sat, 18 Feb 2017 01:06:18 +0100 Subject: [PATCH 2/4] Proper float comparison in ratio validation --- src/Illuminate/Validation/Validator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index e2f055ab6f52..0c1a596e43f6 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -1687,7 +1687,7 @@ protected function validateDimensions($attribute, $value, $parameters) [1, 1], array_filter(sscanf($parameters['ratio'], '%f/%d')) ); - return $numerator / $denominator == $width / $height; + return abs($numerator / $denominator - $width / $height) < 0.000001; } return true; From 447e524d683e2d1540a42fd0722820ccfe9938da Mon Sep 17 00:00:00 2001 From: vlakoff Date: Sat, 18 Feb 2017 01:07:12 +0100 Subject: [PATCH 3/4] Convert fixtures to PNG format --- tests/Validation/ValidationValidatorTest.php | 6 +++--- .../Validation/fixtures/{empty.gif => empty.png} | 0 tests/Validation/fixtures/image.gif | Bin 1097 -> 0 bytes tests/Validation/fixtures/image.png | Bin 0 -> 91 bytes 4 files changed, 3 insertions(+), 3 deletions(-) rename tests/Validation/fixtures/{empty.gif => empty.png} (100%) delete mode 100644 tests/Validation/fixtures/image.gif create mode 100644 tests/Validation/fixtures/image.png diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 560b76fac097..7c8634b70770 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -1855,8 +1855,8 @@ public function testValidateImage() public function testValidateImageDimensions() { - // Knowing that demo image.gif has width = 3 and height = 2 - $uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/image.gif', '', null, null, null, true); + // Knowing that demo image.png has width = 3 and height = 2 + $uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/image.png', '', null, null, null, true); $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => 'file'], ['x' => 'dimensions']); @@ -1905,7 +1905,7 @@ public function testValidateImageDimensions() $this->assertTrue($v->fails()); // This test fails without suppressing warnings on getimagesize() due to a read error. - $emptyUploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/empty.gif', '', null, null, null, true); + $emptyUploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/empty.png', '', null, null, null, true); $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['x' => $emptyUploadedFile], ['x' => 'dimensions:min_width=1']); diff --git a/tests/Validation/fixtures/empty.gif b/tests/Validation/fixtures/empty.png similarity index 100% rename from tests/Validation/fixtures/empty.gif rename to tests/Validation/fixtures/empty.png diff --git a/tests/Validation/fixtures/image.gif b/tests/Validation/fixtures/image.gif deleted file mode 100644 index db5ecb91ea7d6367bed7a68db66371a27786680e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1097 zcmZ?wbhEHbWM*JuXkcLY|NlP&1B2p!?g-xi1((E!Y#3Q(W~w5=#5%__*n4QdyVXRDM^Qc_^0uU}qXu2*iX zmtT~wZ)j<02{OaTNEfI=x41H|B(Xv_uUHvof=g;~a#3bMNoIbY0?5R~r2NtnTP2`N zAzsKWfE$}vtOxdvUUGh}ennz|zM-B0$V)JVzP|XC=H|jx7ncO3BHWAB;NpiyW z)Z+ZoqU2Pda%GTJ1y;^Qsfi`|MIrh5Ij~R+$jC3rFV4s>P;d@5Q_%2DOwP;$321_K z`1)FT<`tJD<|U_ky4WfK&CtutOtCUBH!^fHay2z@G%+_cbTzXyF)(*_GO}>8Fm-b_ zHnfE4b;(aI%}vcKf$2>_=rzNs7nB%s3xGDeq!wkCrKY$Q<>xAZJ#CeV+bza8&4cPq z!R;0koO<#DphYAP1iGQ}cl7y$G1F8UO$L`{(zspFh5T`}*bc zr;i`rzkB=U^{baJoVgsyL#pFrHdENpF4Zz^r@34jvqUE zVojbN~+qz}*ri~lcuUorj^{SOCmM>enWbvYf3+B(8J7@N+nKPzO zn>uCkq=^&y`+9r2yE;4C+ge+in;IMH>uPJNt12tX%Sua%iwXi?qaq{1!$L!Xg8~Em{d|4Ay*xeK-CSLqog5wP?QCtVtt>6f%}h;lT#wKO%<)l^lKl@t}^1_KjQOFV-$0K Date: Sat, 18 Feb 2017 01:08:43 +0100 Subject: [PATCH 4/4] Add test for validation of ratio with no fractional part --- tests/Validation/ValidationValidatorTest.php | 8 ++++++++ tests/Validation/fixtures/image2.png | Bin 0 -> 91 bytes 2 files changed, 8 insertions(+) create mode 100644 tests/Validation/fixtures/image2.png diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 7c8634b70770..3d419ec6c64b 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -1910,6 +1910,14 @@ public function testValidateImageDimensions() $v = new Validator($trans, ['x' => $emptyUploadedFile], ['x' => 'dimensions:min_width=1']); $this->assertTrue($v->fails()); + + // Knowing that demo image2.png has width = 4 and height = 2 + $uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(__DIR__.'/fixtures/image2.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/1']); + $this->assertTrue($v->passes()); } /** diff --git a/tests/Validation/fixtures/image2.png b/tests/Validation/fixtures/image2.png new file mode 100644 index 0000000000000000000000000000000000000000..19f2227c9610c3f2bd59449043ef3e9f0707f9b9 GIT binary patch literal 91 zcmeAS@N?(olHy`uVBq!ia0vp^EI`b}$P6T1)7~opDdqs55ZC|z|1UoxeFDf8_H=O! lsfgn}z{m*XHB9)oei|E46Jz1Snh!t$22WQ%mvv4FO#pyT7YzUa literal 0 HcmV?d00001