diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index 680a856a5be7..0c1a596e43f6 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; } @@ -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; diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 24871e6b2df2..3d419ec6c64b 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']); @@ -1903,6 +1903,21 @@ 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.png', '', null, null, null, true); + $trans = $this->getIlluminateArrayTranslator(); + + $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/empty.png b/tests/Validation/fixtures/empty.png new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tests/Validation/fixtures/image.gif b/tests/Validation/fixtures/image.gif deleted file mode 100644 index db5ecb91ea7d..000000000000 Binary files a/tests/Validation/fixtures/image.gif and /dev/null differ diff --git a/tests/Validation/fixtures/image.png b/tests/Validation/fixtures/image.png new file mode 100644 index 000000000000..739f1454d40a Binary files /dev/null and b/tests/Validation/fixtures/image.png differ diff --git a/tests/Validation/fixtures/image2.png b/tests/Validation/fixtures/image2.png new file mode 100644 index 000000000000..19f2227c9610 Binary files /dev/null and b/tests/Validation/fixtures/image2.png differ