Browse Source

added Image::getWidth()

Fabian Peter Hammerle 7 years ago
parent
commit
140b958d01
2 changed files with 26 additions and 1 deletions
  1. 8 1
      Image.php
  2. 18 0
      tests/ImageTest.php

+ 8 - 1
Image.php

@@ -4,7 +4,9 @@ namespace fphammerle\helpers;
 
 class Image
 {
-    protected $resource = null;
+    use \fphammerle\helpers\PropertyAccessTrait;
+
+    protected $_resource = null;
 
     private function __construct()
     {
@@ -37,6 +39,11 @@ class Image
         return $image;
     }
 
+    public function getWidth()
+    {
+        return imagesx($this->_resource);
+    }
+
     public function getColorAt($x, $y)
     {
         $colors = imagecolorsforindex(

+ 18 - 0
tests/ImageTest.php

@@ -7,6 +7,24 @@ use fphammerle\helpers\colors;
 
 class ImageTest extends \PHPUnit_Framework_TestCase
 {
+    public function getWidthProvider()
+    {
+        return [
+            [__DIR__ . '/data/color.png', 4],
+            [__DIR__ . '/data/chainring.jpg', 1336],
+            ];
+    }
+
+    /**
+     * @dataProvider getWidthProvider
+     */
+    public function testGetWidth($path, $width)
+    {
+        $img = Image::fromFile($path);
+        $this->assertSame($width, $img->getWidth());
+        $this->assertSame($width, $img->width);
+    }
+
     public function getColorAtProvider()
     {
         return [