Display BLOB image Laravel 4

2020-05-07 02:54发布

问题:

I have added png images as BLOB on mysql but when I try to retrieve them, I got them as files but can't display as images.

Below is my code.

//Controller

public function post_news(){
    $image = Input::file('image');
    $filename = $image->getClientOriginalName();
    $base64 = base64_encode($filename);
    $news = new News();
    $news->title = Input::get('title');
    $news->description = Input::get('desc');
    $news->image = $filename;
    $news->save();

  return Redirect::to('news')->withErrors(['This news has been added on your database. You may continue!']);

//Route

Route::get('/', function() {
$news = DB::table('news')->get();
return View::make('home2', compact('news'));

});

//Views

<?php if (isset($news)) { ?>
            @foreach ($news as $row)
        <div class="img-container">
            <div class="imageOne image"> 
                <?php //dd($row->image); ?>
                <img src="data:image/png;base64,{{ chunk_split(base64_encode($row->image)) }}">  
            </div>
.....

回答1:

<img src="data:image/png;base64,{{ chunk_split(base64_encode($posts2year->StudentImage)) }}" height="100" width="100">

I used this in my code and it worked.