From 235019ad29c8a8ce44c2641fa166baf4e7aea405 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 18 Oct 2014 11:52:36 +0200 Subject: [PATCH] added support to decode directly from base64 encoded data --- src/Intervention/Image/AbstractDecoder.php | 13 +++++++++++++ tests/AbstractDecoderTest.php | 13 +++++++++++++ tests/GdSystemTest.php | 11 +++++++++++ tests/ImagickSystemTest.php | 15 ++++++++++++++- 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/Intervention/Image/AbstractDecoder.php b/src/Intervention/Image/AbstractDecoder.php index dbf71666..cc268efd 100644 --- a/src/Intervention/Image/AbstractDecoder.php +++ b/src/Intervention/Image/AbstractDecoder.php @@ -158,6 +158,16 @@ abstract class AbstractDecoder return is_null($data) ? false : true; } + /** + * Determines if current source data is base64 encoded + * + * @return boolean + */ + public function isBase64() + { + return base64_encode(base64_decode($this->data)) === $this->data; + } + /** * Initiates new Image from Intervention\Image\Image * @@ -223,6 +233,9 @@ abstract class AbstractDecoder case $this->isDataUrl(): return $this->initFromBinary($this->decodeDataUrl($this->data)); + case $this->isBase64(): + return $this->initFromBinary(base64_decode($this->data)); + default: throw new Exception\NotReadableException("Image source not readable"); } diff --git a/tests/AbstractDecoderTest.php b/tests/AbstractDecoderTest.php index 2fbe348c..a200bb6f 100644 --- a/tests/AbstractDecoderTest.php +++ b/tests/AbstractDecoderTest.php @@ -121,6 +121,19 @@ class AbstractDecoderTest extends PHPUnit_Framework_TestCase $this->assertFalse($source->isDataUrl()); } + public function testIsBase64() + { + $decoder = $this->getTestDecoder(null); + $this->assertFalse($decoder->isBase64()); + + $decoder = $this->getTestDecoder('random'); + $this->assertFalse($decoder->isBase64()); + + $base64 = "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAGElEQVQYlWM8c+bMfwYiABMxikYVUk8hAHWzA3cRvs4UAAAAAElFTkSuQmCC"; + $decoder = $this->getTestDecoder($base64); + $this->assertTrue($decoder->isBase64()); + } + public function getTestDecoder($data) { return $this->getMockForAbstractClass('\Intervention\Image\AbstractDecoder', array($data)); diff --git a/tests/GdSystemTest.php b/tests/GdSystemTest.php index 320af085..a64bc40c 100644 --- a/tests/GdSystemTest.php +++ b/tests/GdSystemTest.php @@ -56,6 +56,17 @@ class GdSystemTest extends PHPUnit_Framework_TestCase $this->assertEquals(10, $img->getHeight()); } + public function testMakeFromBase64() + { + $img = $this->manager()->make('iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAGElEQVQYlWM8c+bMfwYiABMxikYVUk8hAHWzA3cRvs4UAAAAAElFTkSuQmCC'); + $this->assertInstanceOf('Intervention\Image\Image', $img); + $this->assertInternalType('resource', $img->getCore()); + $this->assertInternalType('int', $img->getWidth()); + $this->assertInternalType('int', $img->getHeight()); + $this->assertEquals(10, $img->getWidth()); + $this->assertEquals(10, $img->getHeight()); + } + public function testCanvas() { $img = $this->manager()->canvas(30, 20); diff --git a/tests/ImagickSystemTest.php b/tests/ImagickSystemTest.php index f01ed6c4..8cf597cc 100644 --- a/tests/ImagickSystemTest.php +++ b/tests/ImagickSystemTest.php @@ -47,7 +47,20 @@ class ImagickSystemTest extends PHPUnit_Framework_TestCase public function testMakeFromDataUrl() { - $str = file_get_contents('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAGElEQVQYlWM8c+bMfwYiABMxikYVUk8hAHWzA3cRvs4UAAAAAElFTkSuQmCC'); + $str = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAGElEQVQYlWM8c+bMfwYiABMxikYVUk8hAHWzA3cRvs4UAAAAAElFTkSuQmCC'; + $img = $this->manager()->make($str); + $this->assertInstanceOf('Intervention\Image\Image', $img); + $this->assertInstanceOf('Imagick', $img->getCore()); + $this->assertInternalType('int', $img->getWidth()); + $this->assertInternalType('int', $img->getHeight()); + $this->assertEquals(10, $img->getWidth()); + $this->assertEquals(10, $img->getHeight()); + $this->assertEquals('image/png', $img->mime); + } + + public function testMakeFromBase64() + { + $str = 'iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAGElEQVQYlWM8c+bMfwYiABMxikYVUk8hAHWzA3cRvs4UAAAAAElFTkSuQmCC'; $img = $this->manager()->make($str); $this->assertInstanceOf('Intervention\Image\Image', $img); $this->assertInstanceOf('Imagick', $img->getCore());