Skip to content

Commit

Permalink
Image: added support for BMP
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 28, 2019
1 parent c227724 commit 711b1ac
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Utils/Image.php
Expand Up @@ -115,11 +115,12 @@ class Image
JPEG = IMAGETYPE_JPEG,
PNG = IMAGETYPE_PNG,
GIF = IMAGETYPE_GIF,
WEBP = 18; // IMAGETYPE_WEBP is available as of PHP 7.1
WEBP = IMAGETYPE_WEBP,
BMP = IMAGETYPE_BMP;

public const EMPTY_GIF = "GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;";

private const FORMATS = [self::JPEG => 'jpeg', self::PNG => 'png', self::GIF => 'gif', self::WEBP => 'webp'];
private const FORMATS = [self::JPEG => 'jpeg', self::PNG => 'png', self::GIF => 'gif', self::WEBP => 'webp', self::BMP => 'bmp'];

/** @var resource */
private $image;
Expand Down Expand Up @@ -569,6 +570,10 @@ private function output(int $type, ?int $quality, string $file = null): void
$success = @imagewebp($this->image, $file, $quality); // @ is escalated to exception
break;

case self::BMP:
$success = @imagebmp($this->image, $file); // @ is escalated to exception
break;

default:
throw new Nette\InvalidArgumentException("Unsupported image type '$type'.");
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Utils/Image.save.phpt
Expand Up @@ -46,6 +46,21 @@ test(function () use ($main) {
});


test(function () use ($main) {
if (!function_exists('imagebmp')) {
return;
}

$main->save(getTempDir() . '/foo.bmp');
Assert::true(is_file(getTempDir() . '/foo.bmp'));
Assert::same(IMAGETYPE_BMP, getimagesize(getTempDir() . '/foo.bmp')[2]);

$main->save(getTempDir() . '/foo.y', null, Image::BMP);
Assert::true(is_file(getTempDir() . '/foo.y'));
Assert::same(IMAGETYPE_BMP, getimagesize(getTempDir() . '/foo.y')[2]);
});


Assert::exception(function () use ($main) { // invalid image type
$main->save('foo', null, IMG_WBMP);
}, Nette\InvalidArgumentException::class, sprintf('Unsupported image type \'%d\'.', IMG_WBMP));
Expand Down

0 comments on commit 711b1ac

Please sign in to comment.