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
| Approach | What changes |
|---|---|
| GD / Imagick / EXIF | GD operates on raster images in PHP; Imagick delegates to ImageMagick; EXIF reads metadata and is not an image decoder. |
| Choosing safely | Check image dimensions, true decoder availability, and actual output format rather than trusting a filename suffix. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Trusting the image extension or MIME header alone | Decode the source and verify the actual format, dimensions, and resource limits. |
| Forgetting transparency or aspect ratio | Preserve alpha for transparent formats and calculate dimensions before resampling. |
| Skipping operational constraints | XBM 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.