PHP Error with ImageCreateFromBMP function

Filed Under PHP/MySQL on 2008-10-14, 13:49

This post is for the web/php geeks out there.
I have a page that needs to be able to accept multiple image formats as the users are not to be expected to format their image just right. In short it takes whatever image the user provides and then converts it to a PNG I can use. In an attempt to support as many image formats as possible, I’ve had to jump through a few hoops. Luckily the GD library with PHP has the following functions: imagecreatefrompng, imagecreatefromjpeg, imagecreatefromgif. However what GD does not have built in is imagecreatefrombmp. Boo! But thanks to PHP.net’s users, there’s a function written by DHKold.

So I added this function to my code, and things seem to be working fine. But lately I noticed that my error logs were huge! We’re talking hundreds of megs. I looked into them and one of the main errors flooding my logs was this:

[Tue Oct 14 01:06:39 2008] [error] PHP Warning: unpack() [<a href='function.unpack'>function.unpack</a>]: Type n: not enough input, need 2, have 0 in /usr/www/path/to/my/file.php on line 157

It was repeating itself several hundred times a second. This was not right. I took a look at my code and line 157 was the unpack call in the following block of code:

elseif ($BMP['bits_per_pixel'] == 16)
{
$COLOR = unpack("n",substr($IMG,$P,2));
$COLOR[1] = $PALETTE[$COLOR[1]+1];
}

Looks like 16bit BMPs aren’t really compatible with this function. *grumble grumble* Image formats are not my forte, and I have zero interest in learning about all the various ins and outs. Luckily the PHP user comments come in handy again! domelca writes:

function ImageCreateFromBMP($filename) don't work with bmp 16 bits_per_pixel
change pixel generator for this
elseif ($BMP['bits_per_pixel'] == 16)
{
$COLOR = unpack("v",substr($IMG,$P,2));
$blue = ($COLOR[1] & 0x001f) << 3;
$green = ($COLOR[1] & 0x07e0) >> 3;
$red = ($COLOR[1] & 0xf800) >> 8;
$COLOR[1] = $red * 65536 + $green * 256 + $blue;
}

I’ve updated the code in my file and now I’m watching the error logs closely to see if anyone’s images trigger the error again. Why PHP doesn’t provide an official imagecreatefrombmp() I don’t know, but hopefully this fix will help support a few more different images as well as keep my error logs a little smaller. Figured I’d post this to save someone else the hassle of hunting it down.

Tagged:


Comments

View Comments to “PHP Error with ImageCreateFromBMP function”

  1. kale on October 14th, 2008 7:29 pm

    One thing to consider is what “warning” means — it's just a warning. If the end product works, it may be acceptable to deal with, especially if you don't feel like rewriting your app, for example. An option to consider is to possibly disable warnings. Fatals and emergencies will still be reported, but that's OK, because that's what matters. Consider editing your php.ini or adding the following line to your code:

    error_reporting(E_ALL ^ E_WARNING);

    (of course, you'll want to add ^E_NOTICE if you get a ton of that in the logs as well ;)

  2. a8 on October 29th, 2008 2:58 am

    It might be acceptable to you but for some that’s a no no besides that eat up some space and if you can do a quick fix then you should fix it otherwise leave it as is.

  3. a8 on October 28th, 2008 10:58 pm

    It might be acceptable to you but for some that's a no no besides that eat up some space and if you can do a quick fix then you should fix it otherwise leave it as is.

  4. Guest on August 31st, 2010 3:41 pm

    Thanks for posting this!

Leave a Reply




blog comments powered by Disqus