php 将blob图像转换为php中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5745278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
converting blob image to file in php
提问by Walker
I'm currently using a simple $_FILES upload script to upload pictures from my iPhone app to server. The image sizes, however, are large and I'd like to resize them before sending the image to the server.
我目前正在使用一个简单的 $_FILES 上传脚本将图片从我的 iPhone 应用程序上传到服务器。但是,图像尺寸很大,我想在将图像发送到服务器之前调整它们的大小。
The downside of this, however, is that the resize function converts them to "blob" images (which, as I understand it, is a way of storing the image in the database). I'd prefer to save the files directly to the filesystem. How would I go about converting a blob back into a $_FILE or finding a script that saves blob images to disc??
然而,这样做的缺点是调整大小函数将它们转换为“blob”图像(据我所知,这是一种将图像存储在数据库中的方法)。我更喜欢将文件直接保存到文件系统。我将如何将 blob 转换回 $_FILE 或找到将 blob 图像保存到光盘的脚本?
Thank you!
谢谢!
回答by Christof
The BLOB isthe binary image. You can write that image to your filesystem once it's on your server. So if your image is in the variable $my_blob, you would do something like
BLOB是二进制图像。一旦它在您的服务器上,您就可以将该图像写入您的文件系统。所以如果你的图像在变量 $my_blob 中,你会做类似的事情
file_put_contents('/path/to/new/file_name', $my_blob);
and there you go.
你去吧。
You might want to save the file to a tmp location first, then do some checks on it before you move it to a final location (with PHPs rename() function).
您可能希望先将文件保存到 tmp 位置,然后在将其移动到最终位置之前对其进行一些检查(使用 PHP 的 rename() 函数)。
Btw: why not just save the BLOB to DB? That is a legitimate way of handling files these days, that's what the BLOB MySQL data type is for after all.
顺便说一句:为什么不直接将 BLOB 保存到 DB?这是当今处理文件的合法方式,毕竟这是 BLOB MySQL 数据类型的用途。
回答by Chris Warrick
Try just writing it to a file with an image/[fill-in-the-blank]
mimetype. Or maybe imagecreatefromstring
will work. (NOT sure about any of these)
尝试将其写入具有image/[fill-in-the-blank]
mimetype的文件。或者也许imagecreatefromstring
会起作用。(不确定其中任何一个)
Or you can find another way to resize the images.
或者您可以找到另一种调整图像大小的方法。