Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/Intervention/Image/AbstractDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ public function __construct($data = null)
*/
public function initFromUrl($url)
{

$options = [
'http' => [
'method'=>"GET",
'header'=>"Accept-language: en\r\n".
"User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2\r\n"
]
];

$context = stream_context_create($options);


if ($data = @file_get_contents($url, false, $context)) {
return $this->initFromBinary($data);
Expand Down Expand Up @@ -265,6 +265,33 @@ public function isBase64()
return base64_encode(base64_decode($this->data)) === $this->data;
}

/**
* Determines if the current source data is a Symfony Storage path
*
* @return boolean
*/
public function isStorage()
{
if (is_string($this->data)) {
return preg_match('/^\w*:[^\/].*$/', $this->data) ? true : false;
}

return false;
}

/**
* Initiates new Image from Symfony Storage
*
* @param string $path
* @return \Intervention\Image\Image
*/
public function initFromStorage($path)
{
preg_match('/(.*):(.*)/', $path, $matches);
list(, $fs, $file) = $matches;
return $this->initFromBinary(\Storage::disk($fs)->get($file));
}

/**
* Initiates new Image from Intervention\Image\Image
*
Expand Down Expand Up @@ -341,6 +368,9 @@ public function init($data)
case $this->isBase64():
return $this->initFromBinary(base64_decode($this->data));

case $this->isStorage():
return $this->initFromStorage($this->data);

default:
throw new Exception\NotReadableException("Image source not readable");
}
Expand Down
16 changes: 15 additions & 1 deletion tests/AbstractDecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public function tearDown()
{
Mockery::close();
}

public function testIsImagick()
{
$source = $this->getTestDecoder(new \Imagick);
Expand Down Expand Up @@ -51,6 +51,7 @@ public function testIsUrl()
{
$source = $this->getTestDecoder('http://foo.bar');
$this->assertTrue($source->isUrl());
$this->assertFalse($source->isStorage());

$source = $this->getTestDecoder(null);
$this->assertFalse($source->isUrl());
Expand Down Expand Up @@ -144,6 +145,19 @@ public function testIsBase64()
$this->assertTrue($decoder->isBase64());
}

public function testIsStorage()
{
$decoder = $this->getTestDecoder(null);
$this->assertFalse($decoder->isStorage());

$decoder = $this->getTestDecoder('random');
$this->assertFalse($decoder->isStorage());

$storage = "public:test";
$decoder = $this->getTestDecoder($storage);
$this->assertTrue($decoder->isStorage());
}

public function getTestDecoder($data)
{
return $this->getMockForAbstractClass('\Intervention\Image\AbstractDecoder', [$data]);
Expand Down