简体   繁体   English

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

[英]How to get bmp image from from binary blob data with php

I'm trying for 2 days to find a way to retrieve some images from a database.我试图用 2 天的时间来找到一种从数据库中检索一些图像的方法。 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.大多数图像都是.jpeg格式,很容易获取并保存到文件中。 but my problem is the .bmp images.但我的问题是.bmp图像。 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..jpeg.png工作正常,但在我尝试显示时无法读取.bmp格式。

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.顺便说一句,我不确定这是否会影响问题,但在我的项目中我使用了这个库https://github.com/Intervention/image ,只有这样我才能看到图像的“半转换”。 With the file_put_contents() it still unreadable.使用file_put_contents()它仍然无法读取。 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 :我在下面的这个解决方案之前使用.bmp检查我拥有的图像是否是.bmp

PHP : binary image data, checking the image type PHP : 二进制图像数据,检查图像类型

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.我的问题是.bmp文件由于某种原因没有显示。

The problem is BLOB which is limited to 65535 bytes.问题是BLOB ,它被限制为 65535 字节。

Bitmaps are roughly 4-8x the size of a compressed image and will most probably need more space to be put into the database.位图大约是压缩图像大小的 4-8 倍,很可能需要更多空间才能放入数据库。

I suggest altering the column to LONGBLOB or at least MEDIUMBLOB .我建议将列更改为LONGBLOB或至少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.您也不需要来自 github 的库,如果图像已完全存储,您可以像使用 png 或 jpeg 一样使用file_put_contents() (这是二进制安全的,是的!)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM