mirror of
https://github.com/Intervention/image.git
synced 2025-08-18 11:41:17 +02:00
added resource stream detection and initialization, with tests
This commit is contained in:
@@ -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);
|
||||
|
||||
|
@@ -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'));
|
||||
|
Reference in New Issue
Block a user