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

Updating Info.php to have an isValid() method, updating tests #54

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
18 changes: 16 additions & 2 deletions src/Handler/Image/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Info implements JsonSerializable, Serializable
protected $mime;
/** @var Exif */
protected $exif;
/** @var boolean */
protected $isValid = true;

/** @var ReaderInterface */
protected static $exifReader;
Expand Down Expand Up @@ -78,7 +80,9 @@ public static function createFromFile($file)
if ($info === false) {
$data = @file_get_contents($file);
if ($data === '' || !static::isSvg($data, $file)) {
return static::createEmpty();
$result = static::createEmpty();
$result->isValid = false;
return $result;
}

return static::createSvgFromString($data);
Expand Down Expand Up @@ -109,7 +113,9 @@ public static function createFromString($data, $filename = null)

$info = @getimagesizefromstring($data);
if ($info === false) {
return static::createEmpty();
$result = static::createEmpty();
$result->isValid = false;
return $result;
}

$file = sprintf('data://%s;base64,%s', $info['mime'], base64_encode($data));
Expand Down Expand Up @@ -426,4 +432,12 @@ public function unserialize($serialized)
$this->mime = $data['mime'];
$this->exif = new Exif($data['exif']);
}

/**
* @return mixed
*/
public function isValid()
{
return $this->isValid;
}
}
10 changes: 6 additions & 4 deletions tests/Handler/Image/InfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public function testCreateFromFileEmpty()

public function testCreateFromFileFail()
{
$this->setExpectedException(IOException::class, 'Failed to get image data from file');
Image\Info::createFromFile('drop-bear.jpg');
$info = Image\Info::createFromFile('drop-bear.jpg');
$this->assertFalse($info->isValid());
}

public function testCreateFromString()
Expand All @@ -91,6 +91,7 @@ public function testCreateFromString()
$this->assertTrue($info->isLandscape());
$this->assertFalse($info->isPortrait());
$this->assertFalse($info->isSquare());
$this->assertTrue($info->isValid());
}

public function testCreateFromStringEmpty()
Expand All @@ -105,12 +106,13 @@ public function testCreateFromStringEmpty()
$this->assertSame(0, $info->getChannels());
$this->assertSame(null, $info->getMime());
$this->assertSame(0.0, $info->getAspectRatio());
$this->assertTrue($info->isValid());
}

public function testCreateFromStringFail()
{
$this->setExpectedException(IOException::class, 'Failed to get image data from string');
Image\Info::createFromString('drop-bear.jpg');
$info = Image\Info::createFromString('drop-bear.jpg');
$this->assertFalse($info->isValid());
}

public function testClone()
Expand Down