WBMP output doesn't work

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hello, I'm having a probelm output wbmp images. Something seems to keep
going wrong with the encoding, because I start with a good jpeg and get
a nonworking wbmp on the other end.

here is my code:

$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width,
$new_height, $width, $height);

// Output new image
imagewbmp($image_p, "..path..");

Replacing the imagewbmp with imagejpeg will output a valid jpeg file,
or I can output a valid PNG as well. But outputing a wbmp never works,
I get a busted image. Anyone know what's going on here? Am I missing
some color conversion step?

Re: WBMP output doesn't work

I checked the gd_info() and wbmp support is enabled, btw.

Re: WBMP output doesn't work

Quoted text here. Click to load it

 OK, in what way is it busted?

 Zero length file? Or actually producing a corrupted file in some way?

 Anything obvious in the file itself like error messages? (Presumably not since
it's using the path argument in the GD call, but worth a quick look).

<http://www.andyhsoftware.co.uk/space Space: disk usage analysis tool

Re: WBMP output doesn't work

AaronV wrote:
Quoted text here. Click to load it

The problem is that WBMP can only be created from images which contain black  
and white only.

The imagetruecolortopalette() function seems to be able to reduce the color  
depth, but the result will still be insufficient to create a WBMP image,  
because it will not consist of just black and white.

One solution is to map the pixels to either black or white manually. You  
will need to to this after you have resampled the image and before you call  

The following code produced reasonable results with the images I have tested  
it with:

for ($y = 0; $y < $new_height; $y++) {
     for ($x = 0; $x < $new_width; $x++) {
          $colors = imagecolorsforindex(
                imagecolorat($image_p, $x, $y)

          if ($colors['red'] > 180) {
               $color = imagecolorallocate($image_p, 255, 255, 255);
          } else {
               $color = imagecolorallocate($image_p, 0, 0, 0);
          imagesetpixel($image_p, $x, $y, $color);

As I said, you should insert this code just after you have called  
imagecopyresampled() and before calling imagewbmp().


Site Timeline