Content · Web Development · PHP

PHP imagewbmp() Function: Usage and Examples

Writes a monochrome GD image in WBMP format to a path or output stream; the image must be suitable for black-and-white encoding.

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

Writes a monochrome GD image in WBMP format to a path or output stream; the image must be suitable for black-and-white encoding.

What It Does and When to Use It


Create a small example image and save it using the exact encoder. 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. A successful call is not a reason to trust an unwritable path or an unusable output file.

Example 1: Basic Operation


Create a small example image and save it using the exact encoder.

<?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.wbmp';
 try { if (!imagewbmp($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.

<?php
$image = imagecreatetruecolor(100, 40);
 $background = imagecolorallocate($image, 240, 240, 240);
 imagefill($image, 0, 0, $background);
 $destination = __DIR__ . '/label.wbmp';
 if (!imagewbmp($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.

<?php
if (!function_exists('imagewbmp')) {
     throw new RuntimeException('Required GD encoder unavailable');
 }
 $image = imagecreatetruecolor(4, 4);
 $destination = sys_get_temp_dir() . '/sample.wbmp';
 try {
     if (!imagewbmp($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. The practical example adds: Verify encoder availability and the file rather than relying on the function name alone.

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 constraintsA successful call is not a reason to trust an unwritable path or an unusable output file.

Key Takeaways


  • Writes a monochrome GD image in WBMP format to a path or output stream; the image must be suitable for black-and-white encoding.
  • Verify encoder availability and the file rather than relying on the function name alone.
  • 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.