Browse Source

Adding image size validation

Florian Krämer 9 years ago
parent
commit
33cc7a9461
2 changed files with 139 additions and 0 deletions
  1. 70 0
      src/Validation/Validation.php
  2. 69 0
      tests/TestCase/Validation/ValidationTest.php

+ 70 - 0
src/Validation/Validation.php

@@ -1082,6 +1082,76 @@ class Validation
     }
 
     /**
+     * Validates the size of an uploaded image.
+     *
+     * @param array $value
+     * @param array $options
+     * @return boolean
+     */
+    public static function imageSize($value, $options)
+    {
+        if (!isset($options['height']) && !isset($options['width'])) {
+            throw new InvalidArgumentException('Invalid image size validation parameters! Missing `width` and / or `height`!');
+        }
+
+        list($width, $height) = getimagesize($value['tmp_name']);
+
+        if (isset($options['height'])) {
+            $validHeight = self::comparison($height, $options['height'][0], $options['height'][1]);
+        }
+        if (isset($options['width'])) {
+            $validWidth = self::comparison($width, $options['width'][0], $options['width'][1]);
+        }
+        if (isset($validHeight) && isset($validWidth)) {
+            return ($validHeight && $validWidth);
+        }
+        if (isset($validHeight)) {
+            return $validHeight;
+        }
+        if (isset($validWidth)) {
+            return $validWidth;
+        }
+
+        throw new InvalidArgumentException('The 2nd argument is missing the `width` and / or `height` options!');
+    }
+
+    /**
+     * Validates the image width.
+     *
+     * @param array $value
+     * @param string $operator
+     * @param integer $width
+     * @return boolean
+     */
+    public static function imageWidth($value, $operator, $width)
+    {
+        return self::imageSize($value, [
+            'width' => [
+                $operator,
+                $width
+            ]
+        ]);
+    }
+
+    /**
+     * Validates the image width.
+     *
+     * @param array $value
+     * @param string $operator
+     * @param integer $height
+     * @return boolean
+     */
+    public static function imageHeight($value, $operator, $height)
+    {
+        return self::imageSize($value, [
+            'height' => [
+                $operator,
+                $height
+            ]
+        ]);
+    }
+
+    /**
      * Validates a geographic coordinate.
      *
      * Supported formats:

+ 69 - 0
tests/TestCase/Validation/ValidationTest.php

@@ -2807,4 +2807,73 @@ class ValidationTest extends TestCase
         $this->assertFalse(Validation::numElements(false, '==', 0));
         $this->assertFalse(Validation::numElements(true, '==', 0));
     }
+
+    /**
+     * Test imageSize
+     *
+     * @return void
+     */
+    public function testImageSize() {
+        $image = WWW_ROOT . 'test_theme' . DS . 'img' . DS . 'test.jpg';
+        $upload = [
+            'tmp_name' => $image
+        ];
+
+        $this->assertTrue(Validation::imageSize($upload, [
+            'width' => ['>', 100],
+            'height' => ['>', 100],
+        ]));
+
+        $this->assertFalse(Validation::imageSize($upload, [
+            'width' => ['>', 100],
+            'height' => ['<', 100],
+        ]));
+
+        $this->assertFalse(Validation::imageSize($upload, [
+            'width' => ['==', 100],
+            'height' => ['==', 300],
+        ]));
+    }
+
+    /**
+     * Test imageHeight
+     *
+     * @return void
+     */
+    public function testImageHeight()
+    {
+        $image = WWW_ROOT . 'test_theme' . DS . 'img' . DS . 'test.jpg';
+        $upload = [
+            'tmp_name' => $image
+        ];
+
+        $this->assertTrue(Validation::imageHeight($upload, '>', 100));
+        $this->assertTrue(Validation::imageHeight($upload, '<', 2000));
+        $this->assertTrue(Validation::imageHeight($upload, '==', 300));
+
+        $this->assertFalse(Validation::imageHeight($upload, '<', 100));
+        $this->assertFalse(Validation::imageHeight($upload, '>', 2000));
+        $this->assertFalse(Validation::imageHeight($upload, '==', 3000));
+    }
+
+    /**
+     * Test imageWidth
+     *
+     * @return void
+     */
+    public function testImageWidth()
+    {
+        $image = WWW_ROOT . 'test_theme' . DS . 'img' . DS . 'test.jpg';
+        $upload = [
+            'tmp_name' => $image
+        ];
+
+        $this->assertTrue(Validation::imageWidth($upload, '>', 100));
+        $this->assertTrue(Validation::imageWidth($upload, '<', 2000));
+        $this->assertTrue(Validation::imageWidth($upload, '==', 300));
+
+        $this->assertFalse(Validation::imageWidth($upload, '<', 100));
+        $this->assertFalse(Validation::imageWidth($upload, '>', 2000));
+        $this->assertFalse(Validation::imageWidth($upload, '==', 3000));
+    }
 }