php – How can I display an image in Laravel 5.3 using *BLOB?-ThrowExceptions

Exception or error:

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' => ??????????               
    ]);
}
How to solve:

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!

Answer´╝Ü

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')}}" />

Leave a Reply

Your email address will not be published. Required fields are marked *