Skip to content

Commit

Permalink
Handle an invalid resource and throw a InvalidArgumentException
Browse files Browse the repository at this point in the history
  • Loading branch information
GwendolenLynch committed Jun 4, 2017
1 parent ae875e0 commit 79f6d57
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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 79f6d57

Please sign in to comment.