2

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.

Community
  • 1
  • 1
Clyff
  • 4,046
  • 2
  • 17
  • 32
  • 1
    If the binary date is in BMP format, and you file_put_contents directly to disk, then you view the file directly -- and it's corrupted -- then the only answer is the original binary data isn't in BMP format. Run `file` on your purported BMP saved to disk and see what it says. – bishop Jun 24 '15 at 15:31
  • 1
    You can't just mix image types. You need to store in the database which format the binary data is and use that one accordingly. – Daniel W. Jun 24 '15 at 15:31
  • @bishop Im sure they are bmp images, The 2 codes i found in the links i mentioned do a check before start converting. – Clyff Jun 24 '15 at 15:39
  • @DanFromGermany I'm sorry, but the original code to store the images isn't mine and i have no acces to it and its not php. If i only have a safe way to read this binary data ( inluding bmp files) that would be awesome. I'm sure someone already have this problem and know how to fix it. – Clyff Jun 24 '15 at 15:42
  • @Clyff fortunately yes, you can use the exif extension and its method `exif_imagetype();` - http://php.net/manual/en/function.exif-imagetype.php (put the blob contents into /tmp/ + random-number) then feed it to the function – Daniel W. Jun 24 '15 at 16:08
  • @DanFromGermany Thanks, but i already edited the question. I'm able to check what image type i'm looking and manage to output the file correctly. My problem is still the bmp files. After i check, i'm unable to save then =/ – Clyff Jun 24 '15 at 16:23
  • 1
    Is it possible that the columns in the database are limited and you don't save the full bmp? Like, a 4 MB Jpeg can be like 30 MB in bmp. A standard blob can only hold 65535 bytes. You need a `MEDIUMBLOB` or `LONGBLOB`. – Daniel W. Jun 24 '15 at 16:30
  • @DanFromGermany OMG thanks dan! I use strlen() to check the bytes and found the problem! All string stop with 64512 bytes and the solution was here http://stackoverflow.com/questions/5635036/data-ended-at-64512-characters-mssql-php-opensuse-apache2. – Clyff Jun 24 '15 at 17:07
  • @DanFromGermany can you put as answer? It helped me ALOT! – Clyff Jun 24 '15 at 17:09

1 Answers1

1

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.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • Thanks again! Using strlen() to count the bytes, I found that ALL strings stop with 64512 bytes. The solution for that problem can be found here http://stackoverflow.com/questions/5635036/data-ended-at-64512-characters-mssql-php-opensuse-apache2 – Clyff Jun 24 '15 at 19:29
  • And i'm using this library because i want to redimension the images, plus it is already being used elsewhere in the project. :) – Clyff Jun 24 '15 at 19:31