Browse Source

added Image::getColorAt(); Image::fromFile(): support png

Fabian Peter Hammerle 7 years ago
parent
commit
da478bd159
5 changed files with 56 additions and 3 deletions
  1. 20 2
      Image.php
  2. 3 1
      colors/RGB.php
  3. 4 0
      colors/RGBA.php
  4. 29 0
      tests/ImageTest.php
  5. BIN
      tests/data/color.png

+ 20 - 2
Image.php

@@ -23,14 +23,32 @@ class Image
      */
     public static function fromFile($path)
     {
+        $image = new self;
         switch(exif_imagetype($path)) {
             case IMAGETYPE_JPEG:
-                $image = new self;
                 $image->_resource = imagecreatefromjpeg($path);
-                return $image;
+                break;
+            case IMAGETYPE_PNG:
+                $image->_resource = imagecreatefrompng($path);
+                break;
             default:
                 throw new \InvalidArgumentException("type of '$path' is not supported");
         }
+        return $image;
+    }
+
+    public function getColorAt($x, $y)
+    {
+        $colors = imagecolorsforindex(
+            $this->_resource,
+            imagecolorat($this->_resource, $x, $y)
+            );
+        return new \fphammerle\helpers\colors\RGBA(
+            $colors['red'] / 0xFF,
+            $colors['green'] / 0xFF,
+            $colors['blue'] / 0xFF,
+            1 - $colors['alpha'] / 127
+            );
     }
 
     /**

+ 3 - 1
colors/RGB.php

@@ -42,7 +42,9 @@ class RGB
     {
         $green = (float)$green;
         if($green < 0 || $green > 1) {
-            throw new \UnexpectedValueException('value must be within [0, 1]');
+            throw new \UnexpectedValueException(
+                sprintf('value must be within [0, 1], %f given', $green)
+                );
         }
         $this->_green = $green;
     }

+ 4 - 0
colors/RGBA.php

@@ -19,6 +19,10 @@ class RGBA extends RGB
         return $this->_alpha;
     }
 
+    /**
+     * alpha 0 => 100% transparency
+     * alpha 1 => 0% transparency, opaque
+     */
     public function setAlpha($alpha)
     {
         $alpha = (float)$alpha;

+ 29 - 0
tests/ImageTest.php

@@ -3,9 +3,38 @@
 namespace fphammerle\helpers\tests;
 
 use fphammerle\helpers\Image;
+use fphammerle\helpers\colors;
 
 class ImageTest extends \PHPUnit_Framework_TestCase
 {
+    public function getColorAtProvider()
+    {
+        return [
+            [__DIR__ . '/data/color.png', 0, 0, new colors\RGBA(0,   1,   0,   1      )],
+            [__DIR__ . '/data/color.png', 1, 0, new colors\RGBA(0,   0,   1,   1      )],
+            [__DIR__ . '/data/color.png', 1, 1, new colors\RGBA(0,   0,   0,   1      )],
+            [__DIR__ . '/data/color.png', 0, 1, new colors\RGBA(1,   0,   0,   1      )],
+            [__DIR__ . '/data/color.png', 0, 2, new colors\RGBA(1,   0.2, 0,   1      )],
+            [__DIR__ . '/data/color.png', 1, 2, new colors\RGBA(0,   1,   0.2, 1      )],
+            [__DIR__ . '/data/color.png', 2, 2, new colors\RGBA(0.2, 0,   1,   1      )],
+            [__DIR__ . '/data/color.png', 3, 2, new colors\RGBA(1,   1,   1,   1      )],
+            [__DIR__ . '/data/color.png', 2, 0, new colors\RGBA(0.2, 0.4, 1,   1      )],
+            [__DIR__ . '/data/color.png', 2, 1, new colors\RGBA(0.2, 0.4, 1,   102/127)],
+            [__DIR__ . '/data/color.png', 3, 1, new colors\RGBA(1,   0.8, 0.2, 102/127)],
+            [__DIR__ . '/data/color.png', 3, 0, new colors\RGBA(0,   0,   0,   0      )],
+            ];
+    }
+
+    /**
+     * @dataProvider getColorAtProvider
+     */
+    public function testGetColorAt($path, $x, $y, $e)
+    {
+        $img = Image::fromFile($path);
+        $r = $img->getColorAt($x, $y);
+        $this->assertTrue($e->equals($r), print_r($r->tuple, true));
+    }
+
     public function rotateProvider()
     {
         return [

BIN
tests/data/color.png