Reputation: 1212
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
Upvotes: 6
Views: 30319
Reputation: 44526
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 = App\User::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 = 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)
));
});
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
Upvotes: 20
Reputation: 577
You can store files in database in blob fields.
Maybe this link helps you
Then You can create url to your image:
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;
});
and get img from url: /image/{img_id}
Upvotes: 0