mirror of
https://github.com/Intervention/image.git
synced 2025-08-29 00:29:55 +02:00
added support to decode directly from base64 encoded data
This commit is contained in:
@@ -158,6 +158,16 @@ abstract class AbstractDecoder
|
|||||||
return is_null($data) ? false : true;
|
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
|
* Initiates new Image from Intervention\Image\Image
|
||||||
*
|
*
|
||||||
@@ -223,6 +233,9 @@ abstract class AbstractDecoder
|
|||||||
case $this->isDataUrl():
|
case $this->isDataUrl():
|
||||||
return $this->initFromBinary($this->decodeDataUrl($this->data));
|
return $this->initFromBinary($this->decodeDataUrl($this->data));
|
||||||
|
|
||||||
|
case $this->isBase64():
|
||||||
|
return $this->initFromBinary(base64_decode($this->data));
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new Exception\NotReadableException("Image source not readable");
|
throw new Exception\NotReadableException("Image source not readable");
|
||||||
}
|
}
|
||||||
|
@@ -121,6 +121,19 @@ class AbstractDecoderTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertFalse($source->isDataUrl());
|
$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)
|
public function getTestDecoder($data)
|
||||||
{
|
{
|
||||||
return $this->getMockForAbstractClass('\Intervention\Image\AbstractDecoder', array($data));
|
return $this->getMockForAbstractClass('\Intervention\Image\AbstractDecoder', array($data));
|
||||||
|
@@ -56,6 +56,17 @@ class GdSystemTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(10, $img->getHeight());
|
$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()
|
public function testCanvas()
|
||||||
{
|
{
|
||||||
$img = $this->manager()->canvas(30, 20);
|
$img = $this->manager()->canvas(30, 20);
|
||||||
|
@@ -47,7 +47,20 @@ class ImagickSystemTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testMakeFromDataUrl()
|
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);
|
$img = $this->manager()->make($str);
|
||||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||||
$this->assertInstanceOf('Imagick', $img->getCore());
|
$this->assertInstanceOf('Imagick', $img->getCore());
|
||||||
|
Reference in New Issue
Block a user