How can I display an image in Laravel 5.3 using *BLOB?

11.6k views Asked by At

To start, I'd like to know how to send an image from my public folder, I have a column image of type MEDIUMBLOB in my table named movies. I'm using mysql_* as my database connection.

This is my current seed:

public function(){
    DB::table('movies')->insert([
            'name' => 'La vida es bella',
            'score' => '0',
            'date' => '1999-2-26',
            'image' => ??????????               
    ]);
}
2

There are 2 answers

1
Jack Hales On

If you're trying to input image data to your MySQL table, use the following structure of code:

DB::table('movies')->insert([
  'image' => file_get_contents( "public/images/image.jpg" )
]);

(I don't use Laravel, or the connection type you're using, that's just the way you insert *BLOB data into a database.

The main function to take away from this is file_get_contents( IMAGE_LOCATION ), which will get the raw image data and uses that as the blob, and if you wanted to pull it from the database you'd just have to pack it into the correct mime, like so:

$blob = ''; //from database
echo '<img src="data:image/jpeg;base64,'.base64_encode( $blob ).'"/>';

This is converting the data to a DATAURI, which you can read about here as a quick-to-read article.

Hope this helps!

0
Rana Nadeem On
echo '<img src="data:image/jpeg;base64,'.chunk_split(base64_encode(file_get_contents($_FILES['userImage']['tmp_name']))).'"/>';

try this code it is working for me,this is in to directly view the blob file which was submitted,

if you want to show the blob image stored in DB then is make a link which return the blob saved in DB and then give that link in src of image

like this

<img src="{{url('url_returning_blob_data_from_db')}}" />