How to get bmp image from from binary blob data with php

2k views Asked by At

I'm trying for 2 days to find a way to retrieve some images from a database. They are saved as a binary string (I read the term somewhere, but frankly never heard before).

Most of the images are in .jpeg format and is easy to get and save to a file. but my problem is the .bmp images. For some reason i can't show then.

For now, i'm using a simple code just to get the image and save it to a file:

$img = $row['image'];
file_put_contents("file.jpeg", $img); //Doesn't matter what format i put there.

Works fine in .jpeg and .png, but .bmp formats are unreadable when i try to display.

Things i already found and didn't solved:

Reason: Both try to convert the image, but some parts are missing, turning black.

Btw, i'm not sure if this will affect the question, but in my project im using this library https://github.com/Intervention/image, and only with this i can see the "semi conversion" of the images. With the file_put_contents() it still unreadable. So my actual code is like this:

$img = Image::make(imagecreatefrombmpstring($item['image']));

$filename = __DIR__ . '/test.jpeg';
$img->save($filename);

EDIT:

I used before this solution below to check if the images i have are .bmp:

PHP : binary image data, checking the image type

And they are. I could easily adapt this and correct my file output format, but this is not my real problem. My problem is the .bmp files, for some reason, are not showing.

1

There are 1 answers

2
Daniel W. On BEST ANSWER

The problem is BLOB which is limited to 65535 bytes.

Bitmaps are roughly 4-8x the size of a compressed image and will most probably need more space to be put into the database.

I suggest altering the column to LONGBLOB or at least MEDIUMBLOB.

You don't need the library from github either, if the image is fully stored, you can use file_put_contents() (which is binary safe, yea!) the same way as with png or jpeg.