【问题标题】:How to store and retrieve image contents from the database using Laravel如何使用 Laravel 从数据库中存储和检索图像内容
【发布时间】:2016-05-27 12:21:15
【问题描述】:

我必须从数据库中存储和检索图像。 我想存储图像而不是图像路径或图像名称。当我搜索它时,我只找到了存储图像路径或名称的解决方案。那么有人可以告诉我如何从数据库中存储和检索大小以 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

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    实际上使用 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)
        ));
    });
    

    因此,要使用等于 10id 访问用户的头像,您只需使用此 URL:

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

    【讨论】:

    • 我尝试使用您的代码,但我得到了格式为“G:\xampp\tmp\phpEA01.tmp”的图像。那么我该如何解决这个问题?你能告诉我如何将它与刀片文件一起使用吗?因为这不适用于 display.blade.php 文件。
    • 此代码有效,我在将其发布为答案之前对其进行了测试。请编辑您的问题以包含您目前使用的代码。
    • 我已经编辑了我的问题。我收到此行 '$img->image = $contents;' 的错误。
    • 所以你是说存储在image 列中的数据是这个G:\xampp\tmp\phpEA01.tmp 而不是二进制数据?我假设您正在尝试存储一个新的 big_image 条目,因此您应该删除 $img = big_image::find($id); 行。
    • 是的。我没有得到二进制数据。好的,但是我收到这个错误 'Creating default object from empty value' 因为 '$img->image = $contents; line.那么我应该如何解决呢?
    【解决方案2】:

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

    也许this link可以帮到你

    然后你可以为你的图片创建网址:

    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 获取 img:/image/{img_id}

    【讨论】:

    • 我已经在数据库中存储了一个图像,它在数据库中保存为 [BLOB - 24 B]。这样对吗?如果是对的,那么我不明白如何显示它?
    • 好的。但是我应该如何在 display.blade.php 中写这个。因为我正在显示这个页面的图像。
    • 使用这个 id )}}"/>
    • 如何在刀片中将图像调整为标准尺寸?
    猜你喜欢
    • 2011-03-21
    • 1970-01-01
    • 2010-12-10
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多