Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

php - How to store and retrieve image contents from the database using Laravel

I have to store and retrieve an image from the database. I want to store an image instead of image path or image name. When I searched for it I only found solutions for storing an image path or name. So can someone tell me how to store and retrieve an image with its size in KBs from database?

This is my route.

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)
));

});

This is my controller

 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'));
}

My display.blade.php file

@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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actually with Laravel it only involves a few lines of code. Let's say you have a user that has an avatar which is stored in the database. Assuming you're using MySQL, here's how you would store and retrieve the avatar from the database:

1. First you'll need to have an avatar column in the users table that can store binary data. Depending on how large you want to allow the avatar image to be, the data type of the column can be one of the following:

BLOB up to 64KB

MEDIUMBLOB up to 16MB

LONGBLOB up to 4GB

2. To store the uploaded image in the database you can do this:

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 = AppUser::find($id);
    $user->avatar = $contents;
    $user->save();
});

3. To fetch and ouput the avatar you can do the following:

Route::get('user/{id}/avatar', function ($id) {
    // Find the user
    $user = AppUser::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)
    ));
});

So to access the avatar of the user with an id equal to 10 you can just use this URL:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...