Content · Web Development · PHP

PHP imagexbm() Function: Usage and Examples

Exports a GD image to XBM, a text-based black-and-white X bitmap representation.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Admin

Exports a GD image to XBM, a text-based black-and-white X bitmap representation.

What It Does and When to Use It


Create a small example image and save it using the exact encoder. XBM is a monochrome text bitmap format and may not be appropriate for photos. GD operates on raster images in PHP; Imagick delegates to ImageMagick; EXIF reads metadata and is not an image decoder.

Requirements, Inputs and Expected Behavior


Image decoding and encoding need the relevant GD, EXIF, or Imagick support; features vary by PHP build and server. XBM is an older monochrome format, not an alternative to photographic JPEG/WebP encoding.

Example 1: Basic Operation


Create a small example image and save it using the exact encoder. XBM is a monochrome text bitmap format and may not be appropriate for photos.

<?php
$image = imagecreate(16, 16);
 $white = imagecolorallocate($image, 255, 255, 255);
 $black = imagecolorallocate($image, 0, 0, 0);
 imageline($image, 0, 0, 15, 15, $black);
 $destination = __DIR__ . '/line.xbm';
 try { if (!imagexbm($image, $destination)) throw new RuntimeException('Image export failed'); }
 finally { imagedestroy($image); }

Example 2: Behavior and Validation


Show the same encoder handling a different source image; ensure the destination is writable. XBM is a monochrome text bitmap format and may not be appropriate for photos.

<?php
$image = imagecreatetruecolor(100, 40);
 $background = imagecolorallocate($image, 240, 240, 240);
 imagefill($image, 0, 0, $background);
 $destination = __DIR__ . '/label.xbm';
 if (!imagexbm($image, $destination)) { throw new RuntimeException('Encoder failed'); }
 imagedestroy($image);

Practical Example: Application Use


Verify encoder availability and the file rather than relying on the function name alone. XBM is a monochrome text bitmap format and may not be appropriate for photos.

<?php
if (!function_exists('imagexbm')) {
     throw new RuntimeException('Required GD encoder unavailable');
 }
 $image = imagecreatetruecolor(4, 4);
 $destination = sys_get_temp_dir() . '/sample.xbm';
 try {
     if (!imagexbm($image, $destination) || !is_file($destination)) throw new RuntimeException('No output generated');
     echo filesize($destination), " bytes
";
 } finally { imagedestroy($image); if (is_file($destination)) unlink($destination); }

Important Behavior and Edge Cases


The second example highlights: Show the same encoder handling a different source image; ensure the destination is writable. XBM is a monochrome text bitmap format and may not be appropriate for photos. The practical example adds: Verify encoder availability and the file rather than relying on the function name alone. XBM is a monochrome text bitmap format and may not be appropriate for photos.

Comparison with Related Approaches


ApproachWhat changes
GD / Imagick / EXIFGD operates on raster images in PHP; Imagick delegates to ImageMagick; EXIF reads metadata and is not an image decoder.
Choosing safelyCheck image dimensions, true decoder availability, and actual output format rather than trusting a filename suffix.

Common Beginner Mistakes


MistakeCorrect approach
Trusting the image extension or MIME header aloneDecode the source and verify the actual format, dimensions, and resource limits.
Forgetting transparency or aspect ratioPreserve alpha for transparent formats and calculate dimensions before resampling.
Skipping operational constraintsXBM is an older monochrome format, not an alternative to photographic JPEG/WebP encoding.

Key Takeaways


  • Exports a GD image to XBM, a text-based black-and-white X bitmap representation.
  • Verify encoder availability and the file rather than relying on the function name alone. XBM is a monochrome text bitmap format and may not be appropriate for photos.
  • Check image dimensions, true decoder availability, and actual output format rather than trusting a filename suffix.
Share this resourceSend this page to someone who may find it useful.