Skip to content

Commit

Permalink
Merge pull request #45 from bolt/hotfix/invalid-images
Browse files Browse the repository at this point in the history
Handle an invalid resource and return an empty GD resource
  • Loading branch information
CarsonF authored Jun 5, 2017
2 parents ae875e0 + 42b64ad commit b036002
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
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

0 comments on commit b036002

Please sign in to comment.