如何使用php从二进制blob数据中获取bmp图像

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.

查看全部
duan198299
duan198299
2015/06/24 15:28
  • image
  • blob
  • bmp
  • php
  • sql
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复