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:
@@ -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(
|
||||||
|
20
src/Drivers/Gd/Encoders/BmpEncoder.php
Normal file
20
src/Drivers/Gd/Encoders/BmpEncoder.php
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
26
src/Drivers/Imagick/Encoders/BmpEncoder.php
Normal file
26
src/Drivers/Imagick/Encoders/BmpEncoder.php
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
33
tests/Drivers/Gd/Encoders/BmpEncoderTest.php
Normal file
33
tests/Drivers/Gd/Encoders/BmpEncoderTest.php
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
36
tests/Drivers/Imagick/Encoders/BmpEncoderTest.php
Normal file
36
tests/Drivers/Imagick/Encoders/BmpEncoderTest.php
Normal 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())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user