Intervention/image · error · EncoderException
Failed to encode bmp format
Error message
Failed to encode bmp format
What it means
The Imagick driver's BmpEncoder clones the native Imagick object, sets format 'BMP' with COMPRESSION_NO (src/Drivers/Imagick/Encoders/BmpEncoder.php:34-38) and serializes with getImagesBlob(). Any ImagickException or ImageException from those calls is rethrown as EncoderException ('Failed to encode bmp format') with the original as previous. The BMP codec ships with virtually every ImageMagick build, so this error usually indicates a stripped-down custom ImageMagick, a resource/policy limit hit on large bitmaps, or a corrupted image core rather than a commonly missing codec.
Source
Thrown at src/Drivers/Imagick/Encoders/BmpEncoder.php:45
*/
public function encode(ImageInterface $image): EncodedImageInterface
{
$format = 'BMP';
$compression = Imagick::COMPRESSION_NO;
try {
$imagick = clone $image->core()->native();
$imagick->setFormat($format);
$imagick->setImageFormat($format);
$imagick->setCompression($compression);
$imagick->setImageCompression($compression);
$result = new EncodedImage($imagick->getImagesBlob(), 'image/bmp');
$imagick->clear();
return $result;
} catch (ImagickException | ImageException $e) {
throw new EncoderException('Failed to encode bmp format', previous: $e);
}
}
}
View on GitHub (pinned to 5598b9e397)
Solutions
- Read $e->getPrevious()->getMessage() to get the exact ImageMagick error text.
- Check php -r "var_dump((new Imagick())->queryFormats('BMP'));" - empty means the BMP coder is absent from this ImageMagick.
- Install the standard distro imagemagick package (which includes the BMP coder) and rebuild the imagick extension against it.
- For very large images, raise PHP memory_limit and ImageMagick policy.xml width/height/area limits, or resize before encoding.
Example fix
// before
$encoded = $image->toBmp(); // EncoderException on stripped ImageMagick builds
// after
if (!in_array('BMP', Imagick::queryFormats('BMP'), true)) {
throw new RuntimeException('BMP encoding unavailable: ImageMagick lacks the BMP coder');
}
$encoded = $image->toBmp(); Defensive patterns
Strategy: try-catch
Validate before calling
if (!in_array('BMP', Imagick::queryFormats('BMP'), true)) {
throw new RuntimeException('BMP encoding unavailable on this ImageMagick build');
} Try / catch
try {
$encoded = $image->toBmp();
} catch (EncoderException $e) {
$reason = $e->getPrevious()?->getMessage() ?? $e->getMessage();
$encoded = $image->toPng(); // BMP is rarely a hard requirement
} Prevention
- Prefer PNG over BMP unless a legacy consumer requires it.
- Cap input dimensions before encoding to avoid uncompressed-buffer blowups.
- Keep the environment on a standard distro ImageMagick rather than custom minimal builds.
When it happens
Trigger: Calling toBmp() or an encodeByExtension/encodeByMime targeting bmp on the Imagick driver when setFormat('BMP')/setImageFormat('BMP') throws (delegate missing in minimal/static builds), or when getImagesBlob() fails - typically on very large images exceeding ImageMagick policy.xml area/memory limits, since BMP output is uncompressed and large.
Common situations: Custom-compiled or embedded ImageMagick builds configured without the BMP coder; Alpine/static builds with a reduced coder list; encoding huge-resolution images where the uncompressed BMP buffer exceeds memory_limit or policy.xml limits.
Related errors
- Failed to encode avif format
- Failed to encode heic format
- Failed to encode jp2 format
- Failed to encode jxl format
- Failed to encode tiff format
AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23).
Data as JSON: /api/errors/3895cf4963bc7086.
Report an issue: GitHub.