1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-27 23:59:50 +02:00

added support to decode directly from base64 encoded data

This commit is contained in:
Oliver Vogel
2014-10-18 11:52:36 +02:00
parent 48777cde5e
commit 235019ad29
4 changed files with 51 additions and 1 deletions

View File

@@ -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");
}

View File

@@ -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));

View File

@@ -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);

View File

@@ -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());