Convert blob image to file in php

I am currently using a simple $ _FILES script download file to upload images from an iPhone application to a server. The image sizes, however, are large, and I would like to resize them before sending the image to the server.

However, the disadvantage of this is that the resize function converts them into a "blob" of the image (which, as I understand it, is a way to store the image in the database). I would prefer to save the files directly to the file system. How could I convert blob back to $ _FILE or find a script that saves blob images to disk?

Thanks!

+6
source share
2 answers

BLOB is a binary image. You can write this image to your file system on your server. Therefore, if your image is in the variable $ my_blob, you would do something like

file_put_contents('/path/to/new/file_name', $my_blob); 

and there you go.

First you can save the file in the tmp location first, then perform some checks before moving it to the final location (with the PHP rename () function).

Btw: why not just save the blob in DB? Nowadays, this is a legitimate way to process files, which is for all MySQL BLOB data types.

+16
source

Try just writing it to a file of type image/[fill-in-the-blank] . Or maybe imagecreatefromstring will work. (NOT sure about this)

Or you can find another way to resize images.

+2
source

Source: https://habr.com/ru/post/886400/


All Articles