首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Laravel从数据库中存储和检索图像内容

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

Stack Overflow用户
提问于 2016-02-16 17:42:17
回答 2查看 26.4K关注 0票数 6

我必须从数据库中存储和检索图像。我想存储一个图像,而不是图像路径或图像名称。当我搜索它时,我只找到了存储图像路径或名称的解决方案。那么,有人能告诉我如何从数据库中存储和检索大小为KB的图像吗?

这是我的路线。

代码语言:javascript
复制
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)
));

});

这是我的控制器

代码语言:javascript
复制
 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文件

代码语言:javascript
复制
@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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-16 19:47:35

实际上,对于Laravel,它只涉及几行代码。假设您有一个用户,其头像存储在数据库中。假设您使用的是MySQL,下面是如何从数据库中存储和检索化身的方法:

1.首先,您需要在users表中有一个可以存储二进制数据的avatar列。根据您希望头像图像的大小,列的数据类型可以是以下类型之一:

最大64KB的

BLOB

高达16MB的MEDIUMBLOB

LONGBLOB高达4 4GB

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

代码语言:javascript
复制
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.获取和输出头像的您可以执行以下操作:

代码语言:javascript
复制
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)
    ));
});

因此,要使用等于10的id访问用户的头像,只需使用以下网址:

代码语言:javascript
复制
http://domain.com/user/10/avatar
票数 17
EN

Stack Overflow用户

发布于 2016-02-16 18:29:06

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

也许this link能帮到你

然后你可以创建指向你的图像的url:

代码语言:javascript
复制
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}获取img

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35428876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档