使用Laravel如何从数据库中存储和检索图像内容

6

我需要将一张图片存储到数据库中,并从数据库中检索该图片。我希望存储的是实际图片内容,而不是图片路径或名称。但当我搜索相关资料时,只发现了关于存储图片路径或名称的解决方案。因此,请问有人能告诉我如何将大小为KB的图片以其实际内容的形式存储并从数据库中检索出来吗?

这是我的路由。

Route::get('big_image/{id}/image', function ($id)
{
   $user = big_image::find($id);
   return response()->make($user->image, 200, array(
   'Content-Type' => (new finfo(FILEINFO_MIME))->buffer($user->image)
));

这是我的控制器

});

 public  function new_store(Request $request ,$id){
    $event_id = $id;
    $img = new big_image;
    $file =  $request->file('image');
    $contents = $file->openFile()->fread($file->getSize());
    $img = big_image::find($id);
    $img->image = $contents;
    $img->image_name = $request->image_name;
    $img->event_id = $event_id;
    $img->save();
  $images=DB::table('big_image')->where('event_id','=',$event_id)->get();
  return view('image_upload',compact('images','id','response'));
}

我的display.blade.php文件

@extends('app')
@section('content')
  <h2 style="text-align: center;margin-top: 10px;">Image Gallery</h2>
  <a href="{{url('pic_upload/'.$id.'')}}" class="col-sm-12"             style="padding-left: 0;"><span> add more pictures</span></a><br>
  <a href="{{url('events')}}"><span>Back to events</span></a><br>
   @foreach($images as $item)
    <div class="col-sm-4">
    {{--<img src="data:image/jpeg;base64,'.base64_encode( $item->image  ).'"/>;--}}
    </div>
@endforeach
@stop
2个回答

19

其实用 Laravel 只需要几行代码就能搞定。假设你有一个用户,他有一个头像存储在数据库中。假设你使用的是 MySQL,以下是如何将头像存储到数据库并从数据库中检索出来:

1. 首先,你需要在 users 表中添加一个可以存储二进制数据的 avatar 列。根据你想要允许头像图像的大小不同,列的数据类型可以是以下之一:

BLOB最大可达 64KB

MEDIUMBLOB 最大可达 16MB

LONGBLOB 最大可达 4GB

2. 要将上传的图片存储到数据库中,你可以这样做:

Route::post('user/{id}', function (Request $request, $id) {
    // Get the file from the request
    $file = $request->file('image');

    // Get the contents of the file
    $contents = $file->openFile()->fread($file->getSize());

    // Store the contents to the database
    $user = App\User::find($id);
    $user->avatar = $contents;
    $user->save();
});

3. 要获取并输出头像,您可以执行以下操作:

Route::get('user/{id}/avatar', function ($id) {
    // Find the user
    $user = App\User::find(1);

    // Return the image in the response with the correct MIME type
    return response()->make($user->avatar, 200, array(
        'Content-Type' => (new finfo(FILEINFO_MIME))->buffer($user->avatar)
    ));
});

要访问一个id等于10的用户头像,您只需使用此网址:

http://domain.com/user/10/avatar

我尝试使用您的代码,但是我得到了一个以这种格式显示的图像 'G:\xampp\tmp\phpEA01.tmp'。那么我该如何解决这个问题?您能告诉我如何在blade文件中使用它吗?因为它不能与display.blade.php文件一起工作。 - Shweta
这段代码是可行的,我在发布回答之前已经测试过了。请编辑您的问题以包含您当前正在使用的代码。 - Bogdan
我已经编辑了我的问题。我在这一行代码“$img->image = $contents;”上遇到了一个错误。 - Shweta
是的,现在错误已经消失了。但是图片没有显示在页面上。页面上什么也没有显示。 - Shweta
让我们在聊天中继续这个讨论 - Shweta
显示剩余5条评论

0

您可以将文件存储在数据库中的 blob 字段中。

也许 这个链接 可以帮助您

然后,您可以创建图像的 URL:

Route::get('/images/{image}', function($image)
{
    header("Content-type: image/jpeg");
    //"select image from table where id = $image"
   echo $here_I_get_my_img_from_DB;
});

从URL获取图片:/image/{img_id}


我已经将一张图片存入数据库,它以 [BLOB - 24 B] 的形式保存在数据库中。这是正确的方式吗?如果是正确的方式,那么我不知道如何显示它? - Shweta
好的。但是我应该如何在我的display.blade.php文件中编写它呢?因为我要从这个页面显示一张图片。 - Shweta
使用此代码 <img src="{{url('/image/' . $item->id )}}"/>。 - Yurich
如何在Blade中将图像调整为标准大小? - joun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接