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
| 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 | A 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.