1
0
mirror of https://github.com/Intervention/image.git synced 2025-09-02 18:32:56 +02:00

Merge pull request #984 from koenvu/master

Add support for bmp
This commit is contained in:
Oliver Vogel
2021-07-05 18:38:07 +02:00
committed by GitHub
3 changed files with 40 additions and 12 deletions

View File

@@ -55,9 +55,25 @@ class Decoder extends \Intervention\Image\AbstractDecoder
$core = @imagecreatefromwebp($path);
break;
case 'image/bmp':
case 'image/ms-bmp':
case 'image/x-bitmap':
case 'image/x-bmp':
case 'image/x-ms-bmp':
case 'image/x-win-bitmap':
case 'image/x-windows-bmp':
case 'image/x-xbitmap':
if (! function_exists('imagecreatefrombmp')) {
throw new NotReadableException(
"Unsupported image type. GD/PHP installation does not support BMP format."
);
}
$core = @imagecreatefrombmp($path);
break;
default:
throw new NotReadableException(
sprintf("Unsupported image type %s. GD driver is only able to decode JPG, PNG, GIF or WebP files.", strtolower($mime))
sprintf("Unsupported image type %s. GD driver is only able to decode JPG, PNG, GIF, BMP or WebP files.", strtolower($mime))
);
}

View File

@@ -96,9 +96,19 @@ class Encoder extends \Intervention\Image\AbstractEncoder
*/
protected function processBmp()
{
throw new NotSupportedException(
"BMP format is not supported by Gd Driver."
);
if ( ! function_exists('imagebmp')) {
throw new NotSupportedException(
"BMP format is not supported by PHP installation."
);
}
ob_start();
imagebmp($this->image->getCore());
$this->image->mime = defined('IMAGETYPE_BMP') ? image_type_to_mime_type(IMAGETYPE_BMP) : 'image/bmp';
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
/**

View File

@@ -100,16 +100,18 @@ class EncoderTest extends TestCase
$this->assertInstanceOf('Intervention\Image\Image', $img);
}
/**
* @expectedException \Intervention\Image\Exception\NotSupportedException
*/
public function testProcessBmpGd()
{
$core = imagecreatefromjpeg(__DIR__.'/images/test.jpg');
$encoder = new GdEncoder;
$image = Mockery::mock('\Intervention\Image\Image');
$img = $encoder->process($image, 'bmp', 90);
$this->assertInstanceOf('Intervention\Image\Image', $img);
if (function_exists('imagebmp')) {
$core = imagecreatefromjpeg(__DIR__.'/images/test.jpg');
$encoder = new GdEncoder;
$image = Mockery::mock('\Intervention\Image\Image');
$image->shouldReceive('getCore')->once()->andReturn($core);
$image->shouldReceive('setEncoded')->once()->andReturn($image);
$img = $encoder->process($image, 'bmp', 90);
$this->assertInstanceOf('Intervention\Image\Image', $img);
$this->assertEquals('image/x-ms-bmp; charset=binary', $this->getMime($encoder->result));
}
}
/**