Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle an invalid resource and return an empty GD resource #45

Merged
merged 2 commits into from
Jun 5, 2017
Merged
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
21 changes: 8 additions & 13 deletions src/Creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,18 @@ public function create(Transaction $transaction)
*/
protected function verifyInfo(Transaction $transaction)
{
try {
$transaction->getSrcImage()->getInfo();

if ($transaction->getSrcImage()->getInfo()->isValid()) {
return;
} catch (IOException $e) {
}

$transaction->setSrcImage($transaction->getErrorImage());
try {
$transaction->getSrcImage()->getInfo();
} catch (IOException $e) {
throw new RuntimeException(
'There was an error with the thumbnail image requested and additionally the fallback image could not be displayed.',
1,
$e
);
if ($transaction->getSrcImage()->getInfo()->isValid()) {
return;
}

throw new RuntimeException(
'There was an error with the thumbnail image requested and additionally the fallback image could not be displayed.',
1
);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/ImageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ public static function createFromFile($file)
public static function createFromString($data)
{
$info = Image\Info::createFromString($data);
$resource = imagecreatefromstring($data);
$resource = @imagecreatefromstring($data);
if ($resource === false) {
throw new InvalidArgumentException('Invalid image data');
}

return new static($resource, null, $info);
}
Expand Down
12 changes: 11 additions & 1 deletion tests/ImageResourceTests.php → tests/ImageResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Bolt\Thumbs\ImageResource;
use Bolt\Thumbs\Point;

class ImageResourceTests extends \PHPUnit_Framework_TestCase
class ImageResourceTest extends \PHPUnit_Framework_TestCase
{
/** @var Filesystem */
protected $fs;
Expand Down Expand Up @@ -46,6 +46,16 @@ public function testExifOrientation()
}
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid image data
*/
public function testInvalidImageFromString()
{
ImageResource::createFromString('');
$this->addToAssertionCount(1);
}

protected function assertDimensions(Dimensions $expected, Dimensions $actual)
{
$this->assertEquals($expected, $actual, "Expected dimension $expected does not equal actual $actual");
Expand Down