1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-30 17:19:50 +02:00

Add encoders for MS Windows Bitmap format (BMP)

This commit is contained in:
Oliver Vogel
2022-08-23 20:21:00 +02:00
parent 996560fff2
commit 4374d15931
5 changed files with 127 additions and 0 deletions

View File

@@ -82,6 +82,18 @@ abstract class AbstractImage implements ImageInterface
); );
} }
public function toBitmap(): EncodedImage
{
return $this->encode(
$this->resolveDriverClass('Encoders\BmpEncoder')
);
}
public function toBmp(): EncodedImage
{
return $this->toBitmap();
}
public function greyscale(): ImageInterface public function greyscale(): ImageInterface
{ {
return $this->modify( return $this->modify(

View File

@@ -0,0 +1,20 @@
<?php
namespace Intervention\Image\Drivers\Gd\Encoders;
use Intervention\Image\Drivers\Abstract\Encoders\AbstractEncoder;
use Intervention\Image\EncodedImage;
use Intervention\Image\Interfaces\EncoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
class BmpEncoder extends AbstractEncoder implements EncoderInterface
{
public function encode(ImageInterface $image): EncodedImage
{
$data = $this->getBuffered(function () use ($image) {
imagebmp($image->getFrame()->getCore(), null, false);
});
return new EncodedImage($data, 'image/bmp');
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Intervention\Image\Drivers\Imagick\Encoders;
use Imagick;
use Intervention\Image\Drivers\Abstract\Encoders\AbstractEncoder;
use Intervention\Image\EncodedImage;
use Intervention\Image\Interfaces\EncoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
class BmpEncoder extends AbstractEncoder implements EncoderInterface
{
public function encode(ImageInterface $image): EncodedImage
{
$format = 'bmp';
$compression = Imagick::COMPRESSION_NO;
$imagick = $image->getFrame()->getCore();
$imagick->setFormat($format);
$imagick->setImageFormat($format);
$imagick->setCompression($compression);
$imagick->setImageCompression($compression);
return new EncodedImage($imagick->getImagesBlob(), 'image/bmp');
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace Intervention\Image\Tests\Drivers\Gd\Encoders;
use Intervention\Image\Collection;
use Intervention\Image\Drivers\Gd\Encoders\BmpEncoder;
use Intervention\Image\Drivers\Gd\Frame;
use Intervention\Image\Drivers\Gd\Image;
use Intervention\Image\Tests\TestCase;
use Intervention\MimeSniffer\MimeSniffer;
use Intervention\MimeSniffer\Types\ImageBmp;
/**
* @requires extension gd
* @covers \Intervention\Image\Drivers\Gd\Encoders\PngEncoder
*/
class BmpEncoderTest extends TestCase
{
protected function getTestImage(): Image
{
$frame = new Frame(imagecreatetruecolor(3, 2));
return new Image(new Collection([$frame]));
}
public function testEncode(): void
{
$image = $this->getTestImage();
$encoder = new BmpEncoder();
$result = $encoder->encode($image);
$this->assertTrue(MimeSniffer::createFromString($result)->matches(new ImageBmp));
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Intervention\Image\Tests\Drivers\Imagick\Encoders;
use Imagick;
use ImagickPixel;
use Intervention\Image\Drivers\Imagick\Encoders\BmpEncoder;
use Intervention\Image\Drivers\Imagick\Image;
use Intervention\Image\Tests\TestCase;
use Intervention\MimeSniffer\MimeSniffer;
use Intervention\MimeSniffer\Types\ImageBmp;
/**
* @requires extension imagick
* @covers \Intervention\Image\Drivers\Imagick\Encoders\JpegEncoder
*/
class BmpEncoderTest extends TestCase
{
protected function getTestImage(): Image
{
$imagick = new Imagick();
$imagick->newImage(3, 2, new ImagickPixel('red'), 'png');
return new Image($imagick);
}
public function testEncode(): void
{
$image = $this->getTestImage();
$encoder = new BmpEncoder();
$result = $encoder->encode($image);
$this->assertTrue(
MimeSniffer::createFromString($result)->matches(new ImageBmp())
);
}
}