All formats

Intervention Image

LanguagePHP 8.3 or later
LicenseMIT
Repositorygithub.com/Intervention/image
Documentationimage.intervention.io
Depends onext-gd or ext-imagick
Read / writeyes / 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#