converting blob image to file in php - php

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.
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??
Thank you!

The BLOB is the 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
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).
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.

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)
Or you can find another way to resize the images.

Related

Figuring out where image data is being stored

Well in a particular website when I upload the image and then try to get the link of the image I get this:
data:image/svg+xml;base64,thebase64encodeOfMyImageCode
I have seen this in many of the website. After decoding the base64 code I find that its the code of my image file (guessed out by uploading svg image file ) . Can any one tell me how this work and where the image is actually being stored . There is no direct URL like website.com/image.svg etc. This is confusing me.
This image is not stored in a file. All the data for the image is stored in the data URL. The server most likely is storing this data in a database, which is then outputted in the webpage inline.
For more information, see MDN
When base64 encoding is used for the image - you simply cannot know where the actual image is stored, if it is available at all. It might not be stored on a file at all, but only on a database, or might be stored outside the domain. On some occasions, such images are generated in real time and not stored on a disk at all.

What should I use to save an image in database

What should I use to save an image in the database. In what type I have to use to store and retrieve the image in PHP and MySQL.
Rule of thumb is you shouldn't be storing images in the database. Blob is available but is pretty crappy.
What you should be doing is storing the binary file on the filesystem which is many times faster than a database and in the database just store a path or link or file name and have the application load the image from the path instead.
This allows you to easily implement stuff like cdn's or san storage etc for static files. It even allows you to use something like lighttpd to display the static content images rather than apache.
https://blogs.oracle.com/manveen/entry/blob_vs_file_system_storage gives a little more information but there's plenty of stats and data on the web about the disadvantages of blobs
use BLOB Datatype which means Binary Large Object
BLOB
BLOB's may be slower than file storage, but it is much easier to copy, backup, and restore a single database file than it is to manage/maintain images in a file structure.
I also recommend storing the images in their own dedicated database (store the properties/tag information in a separate database).
Simple & optimized option is to upload the image on the server and store the string of "url of the image" in database.
Incase, you want to store actual image file only, then use Blob datatype.
Its a very bad idea to store images in database. Database is determined to save a little pieces of information - not images or files. You have HDD on that purpose.
Instead, you should save image on disk and save its filename in database. Like that:
$name = uniqid();
file_put_contents($name.".png",$image_data);
mysql_query("INSERT INTO `images` (image) VALUES ('$name.png')") or die(mysql_error());
If you really insist on saving images in DB use BLOB.

In web industry, how do you save image? binary or local file?

Say I'm using php+mysql.
I can use:
ob_start();
readfile($imgurl);
$img = ob_get_contents();
ob_end_clean();
to catch the binary string of a picture.
I can save the binary string into mysql as imagedata, and use php to create a image object every time I want to display the image. Or I can use php file system functions to write a local image file using the binary string.
Which way is common in website industry? I assume latter is more popular, cuz you can reduce the traffic connecting your database and for some websites, you can open the original image file with a click on the image. I guess they are saving images as local files. Am I right?
Thx.
The common way is to save the images in directory and store the path in database. So while accessing you retrieve just the path.

codeigniter image reader

hello i am trying to use Codeigniter to build a simple user image gallery where a user can upload an image and then this will be auto displayed on another page.
I have the image uploading done as explain on the user guide on Codeigniter and this works fine dumps the image in a folder in the root just placing the URL in one table area and echoing this out works but this will not cover all image names.
what I would like to know is there a way of reading what the user has uploaded and auto storing this in the database to be echoed out.
if not what's the best practice way to do this?
ps new to codeigniter framework but quite familiar to PHP mysql many thanks.
When uploading data using the uploading class you can retrieve information about the uploaded file using the following:
$this->upload->data();
This will return an array containing the name, path, dimensions, etc.
I am not totally familiar with the Codeigniter framework, but from a standard PHP/MySQL perspective this can be done using a BLOB field with MySQL. Simply fread() the uploaded image (addslashes to the content before sending the data) and send that binary output to the BLOB field with a standard SQL query. After that, you will probably need to create a separate file to actually call the image back (PHP will need to reconstruct the image from the data). Familiarize yourself with the HTTP-HEADER "Content-Type:" and how it works for images.
For instance, if you stored a .jpeg image to the database, you will want to set
header("Content-type: image/jpeg");
echo $query_string_array['Image_Data_Row'];
This would need to be in its own file (or part of file ?image=some_img) so it can display only the image. But that is the basic concept of storing an image to the database; you simply break it down, store the information, and reconstruct it when you need it.
Regards,
Dennis M.
As Yorick alluded to: after uploading the file, you can access the file path with the $this->upload->data() method. Specifically, $this->upload->data('full_path'). After the upload, assign this to a variable, then send it to a model or insert the path directly into the db. This will be the full file path to the image on your system. If you want a http accessible path stored instead, you can do something like:
$this->load->helper('url');//load url helper if not autoloaded
$fileName = $this->upload->data('file_name');
$httpPath = base_url().'uploadFolder/'.$fileName;
$this->db->insert('image_table',array('path'=>$httpPath));
HTH

How to store thumbnail image into the database?

I created an .png image from a video using ffmpeg tool and i want to how to insert that image into the db which has a blob field and also i want to know how can i recollect that image from the db to display as a image again?
use file_get_contents($fileDir); and then insert into a text column type in your database ensuring that you base64_encode your file_get_contents string before inserting.
It's called BLOB inserting.
In general it is not recommende to store images in the db. It is usually better to store them in the fs and only keep the meta data in the db.
It's generally a better idea to only store the filename or path to the image in the database rather than storing it as a blob. You'll notice a big performance hit when you have a large volume of images.
Aside from that, it's a lot easier and more secure to use php's built-in file and image functions on actual files, rather than having to process the blob beforehand.

Categories