Intervention Image
| Language | PHP 8.3 or later |
|---|---|
| License | MIT |
| Repository | github.com/Intervention/image |
| Documentation | image.intervention.io |
| Depends on | ext-gd or ext-imagick |
| Read / write | yes / yes, through the driver (GD: 24-bit; Imagick: ImageMagick's BMP writer) |
Intervention Image wraps GD or Imagick behind one object-oriented API: decode an image, chain operations like scale, cover and crop, then encode to a format. BMP is one of its supported formats (Format::BMP, BmpEncoder), so the same code converts BMP to and from PNG, JPEG or WebP.
What BMP variants you can read and write depends on the driver: GD reads RLE and writes 24-bit files, Imagick handles every variant ImageMagick supports. This page shows version 4 of the library.
Installation#
Shell
composer require intervention/image
Basic usage: read, resize and save a BMP#
PHP
<?php
require 'vendor/autoload.php';
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
$manager = ImageManager::usingDriver(Driver::class);
$image = $manager->decodePath('in.bmp');
echo $image->width() . ' x ' . $image->height() . "\n";
$image->scale(width: 640); // keeps the aspect ratio
$image->save('out.bmp'); // format from the extension
How to convert BMP to PNG or JPEG#
PHP
<?php
use Intervention\Image\Format;
$image = $manager->decodePath('in.bmp');
$image->encodeUsingFormat(Format::PNG)->save('out.png');
$image->encodeUsingFormat(Format::JPEG, quality: 85)->save('out.jpg');
How to convert PNG to BMP#
BMP has no reliable transparency, so fill transparent areas first:
PHP
<?php
use Intervention\Image\Encoders\BmpEncoder;
$image = $manager->decodePath('in.png');
$image->fillTransparentAreas('ffffff');
$image->encode(new BmpEncoder())->save('out.bmp');
How to return a BMP in an HTTP response#
PHP
<?php
use Intervention\Image\Format;
$encoded = $manager->decodePath('in.png')->encodeUsingFormat(Format::BMP);
header('Content-Type: ' . $encoded->mediaType());
echo $encoded->toString();
Tips#
- To get the size of a BMP without decoding pixels, use PHP’s
getimagesize(). $manager->decode($input)accepts a path, binary string, base64, data URI orSplFileInfo, e.g. an upload.- With the Imagick driver (
Intervention\Image\Drivers\Imagick\Driver) the output uses ImageMagick’s default BMP writer.