diff --git a/src/Intervention/Image/AbstractDecoder.php b/src/Intervention/Image/AbstractDecoder.php index efdb7489..714797ab 100644 --- a/src/Intervention/Image/AbstractDecoder.php +++ b/src/Intervention/Image/AbstractDecoder.php @@ -70,6 +70,23 @@ abstract class AbstractDecoder ); } + /** + * Init from given stream + * + * @param $stream + * @return \Intervention\Image\Image + */ + public function initFromStream($stream) + { + if ($data = @stream_get_contents($stream)){ + return $this->initFromBinary($data); + } + + throw new \Intervention\Image\Exception\NotReadableException( + "Unable to init from given stream" + ); + } + /** * Determines if current source data is GD resource * @@ -148,6 +165,19 @@ abstract class AbstractDecoder return (bool) filter_var($this->data, FILTER_VALIDATE_URL); } + /** + * Determines if current source data is a stream resource + * + * @return boolean + */ + public function isStream() + { + if (!is_resource($this->data)) return false; + if (get_resource_type($this->data) !== 'stream') return false; + + return true; + } + /** * Determines if current source data is binary data * @@ -244,6 +274,9 @@ abstract class AbstractDecoder case $this->isUrl(): return $this->initFromUrl($this->data); + case $this->isStream(): + return $this->initFromStream($this->data); + case $this->isFilePath(): return $this->initFromPath($this->data); diff --git a/tests/AbstractDecoderTest.php b/tests/AbstractDecoderTest.php index a200bb6f..a55a4439 100644 --- a/tests/AbstractDecoderTest.php +++ b/tests/AbstractDecoderTest.php @@ -58,6 +58,15 @@ class AbstractDecoderTest extends PHPUnit_Framework_TestCase $this->assertFalse($source->isUrl()); } + public function testIsStream() + { + $source = $this->getTestDecoder(fopen(__DIR__ . '/images/test.jpg', 'r')); + $this->assertTrue($source->isStream()); + + $source = $this->getTestDecoder(null); + $this->assertFalse($source->isStream()); + } + public function testIsBinary() { $source = $this->getTestDecoder(file_get_contents(__DIR__.'/images/test.jpg'));