1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-18 19:51:22 +02:00

added resource stream detection and initialization, with tests

This commit is contained in:
Ramiro Araujo
2015-04-10 15:08:13 -03:00
parent 7edc830d19
commit e5259db76b
2 changed files with 42 additions and 0 deletions

View File

@@ -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 * Determines if current source data is GD resource
* *
@@ -148,6 +165,19 @@ abstract class AbstractDecoder
return (bool) filter_var($this->data, FILTER_VALIDATE_URL); 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 * Determines if current source data is binary data
* *
@@ -244,6 +274,9 @@ abstract class AbstractDecoder
case $this->isUrl(): case $this->isUrl():
return $this->initFromUrl($this->data); return $this->initFromUrl($this->data);
case $this->isStream():
return $this->initFromStream($this->data);
case $this->isFilePath(): case $this->isFilePath():
return $this->initFromPath($this->data); return $this->initFromPath($this->data);

View File

@@ -58,6 +58,15 @@ class AbstractDecoderTest extends PHPUnit_Framework_TestCase
$this->assertFalse($source->isUrl()); $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() public function testIsBinary()
{ {
$source = $this->getTestDecoder(file_get_contents(__DIR__.'/images/test.jpg')); $source = $this->getTestDecoder(file_get_contents(__DIR__.'/images/test.jpg'));