Skip to content

Commit ebb7bd5

Browse files
authored
Merge pull request #177 from romanzaycev/master
2 parents def5e5c + 7499d39 commit ebb7bd5

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

lib/ImageResize.php

+38
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public function __construct($filename)
105105
if (!defined('IMAGETYPE_WEBP')) {
106106
define('IMAGETYPE_WEBP', 18);
107107
}
108+
109+
if (!defined('IMAGETYPE_BMP')) {
110+
define('IMAGETYPE_BMP', 6);
111+
}
112+
108113
if ($filename === null || empty($filename) || (substr($filename, 0, 5) !== 'data:' && !is_file($filename))) {
109114
throw new ImageResizeException('File does not exist');
110115
}
@@ -129,6 +134,10 @@ public function __construct($filename)
129134

130135
if (!$checkWebp) {
131136
if (!$image_info) {
137+
if (strstr(finfo_file($finfo, $filename), 'image') !== false) {
138+
throw new ImageResizeException('Unsupported image type');
139+
}
140+
132141
throw new ImageResizeException('Could not read file');
133142
}
134143

@@ -162,6 +171,13 @@ public function __construct($filename)
162171

163172
break;
164173

174+
case IMAGETYPE_BMP:
175+
if (version_compare(PHP_VERSION, '7.2.0', '<')) {
176+
throw new ImageResizeException('For bmp support PHP >= 7.2.0 is required');
177+
}
178+
$this->source_image = imagecreatefrombmp($filename);
179+
break;
180+
165181
default:
166182
throw new ImageResizeException('Unsupported image type');
167183
}
@@ -170,6 +186,8 @@ public function __construct($filename)
170186
throw new ImageResizeException('Could not load image');
171187
}
172188

189+
finfo_close($finfo);
190+
173191
return $this->resize($this->getSourceWidth(), $this->getSourceHeight());
174192
}
175193

@@ -291,6 +309,22 @@ public function save($filename, $image_type = null, $quality = null, $permission
291309
imagecolortransparent($dest_image, $background);
292310
imagefill($dest_image, 0, 0, $background);
293311
break;
312+
313+
case IMAGETYPE_BMP:
314+
if (version_compare(PHP_VERSION, '7.2.0', '<')) {
315+
throw new ImageResizeException('For WebP support PHP >= 7.2.0 is required');
316+
}
317+
318+
if(!empty($exact_size) && is_array($exact_size)) {
319+
$dest_image = imagecreatetruecolor($exact_size[0], $exact_size[1]);
320+
$background = imagecolorallocate($dest_image, 255, 255, 255);
321+
imagefilledrectangle($dest_image, 0, 0, $exact_size[0], $exact_size[1], $background);
322+
} else {
323+
$dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight());
324+
$background = imagecolorallocate($dest_image, 255, 255, 255);
325+
imagefilledrectangle($dest_image, 0, 0, $this->getDestWidth(), $this->getDestHeight(), $background);
326+
}
327+
break;
294328
}
295329

296330
imageinterlace($dest_image, $this->interlace);
@@ -361,6 +395,10 @@ public function save($filename, $image_type = null, $quality = null, $permission
361395

362396
imagepng($dest_image, $filename, $quality);
363397
break;
398+
399+
case IMAGETYPE_BMP:
400+
imagebmp($dest_image, $filename, $quality);
401+
break;
364402
}
365403

366404
if ($permissions) {

test/ImageResizeTest.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ class ImageResizeTest extends TestCase
1010
private $image_types = array(
1111
'gif',
1212
'jpeg',
13-
'png'
13+
'png',
14+
'bmp',
1415
);
1516

16-
private $unsupported_image = 'Qk08AAAAAAAAADYAAAAoAAAAAQAAAAEAAAABABAAAAAAAAYAAAASCwAAEgsAAAAAAAAAAAAA/38AAAAA';
17+
private $unsupported_image = 'AAAKAAAAAAAAAAAAAQABABgAAF9SlQAAAAAAAAAAVFJVRVZJU0lPTi1YRklMRS4A';
1718
private $image_string = 'R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw==';
1819
private $data_url = 'data:image/gif;base64,R0lGODlhAQABAIAAAAQCBP///yH5BAEAAAEALAAAAAABAAEAAAICRAEAOw==';
1920

@@ -67,6 +68,15 @@ public function testLoadWebp()
6768
$this->assertInstanceOf('\Gumlet\ImageResize', $resize);
6869
}
6970

71+
public function testLoadBmp()
72+
{
73+
$image = __DIR__ . '/ressources/test_bmp.bmp';
74+
$resize = new ImageResize($image);
75+
76+
$this->assertEquals(IMAGETYPE_BMP, $resize->source_type);
77+
$this->assertInstanceOf('\Gumlet\ImageResize', $resize);
78+
}
79+
7080
public function testLoadString()
7181
{
7282
$resize = ImageResize::createFromString(base64_decode($this->image_string));
@@ -394,6 +404,15 @@ public function testSavePng()
394404
$this->assertEquals(IMAGETYPE_PNG, exif_imagetype($filename));
395405
}
396406

407+
public function testSaveBmp()
408+
{
409+
$image = $this->createImage(200, 100, 'png');
410+
$resize = new ImageResize($image);
411+
$filename = $this->getTempFile();
412+
$resize->save($filename, IMAGETYPE_BMP);
413+
$this->assertEquals(IMAGETYPE_BMP, exif_imagetype($filename));
414+
}
415+
397416
public function testSaveChmod()
398417
{
399418
$image = $this->createImage(200, 100, 'png');

test/ressources/test_bmp.bmp

142 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)