diff --git a/src/Creator.php b/src/Creator.php index 51c9214..da2f0e9 100644 --- a/src/Creator.php +++ b/src/Creator.php @@ -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 + ); } /** diff --git a/src/ImageResource.php b/src/ImageResource.php index 1a3787b..468ad26 100644 --- a/src/ImageResource.php +++ b/src/ImageResource.php @@ -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); } diff --git a/tests/ImageResourceTests.php b/tests/ImageResourceTest.php similarity index 81% rename from tests/ImageResourceTests.php rename to tests/ImageResourceTest.php index 4fd90e7..a36260f 100644 --- a/tests/ImageResourceTests.php +++ b/tests/ImageResourceTest.php @@ -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; @@ -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");