php 如何使用 Laravel 从数据库中存储和检索图像内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35428876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store and retrieve image contents from the database using Laravel
提问by Shweta
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?
我必须从数据库中存储和检索图像。我想存储图像而不是图像路径或图像名称。当我搜索它时,我只找到了存储图像路径或名称的解决方案。那么有人可以告诉我如何从数据库中存储和检索大小以 KB 为单位的图像吗?
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
我的 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
回答by Bogdan
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:
实际上,Laravel 只涉及几行代码。假设您有一个用户,该用户的头像存储在数据库中。假设您正在使用 MySQL,以下是您将如何从数据库中存储和检索头像:
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:
1.首先,您需要avatar
在users
表中有一列可以存储二进制数据。根据您希望允许头像图像的大小,列的数据类型可以是以下之一:
BLOBup to 64KB
MEDIUMBLOBup to 16MB
LONGBLOBup to 4GB
BLOB高达 64KB
MEDIUMBLOB高达 16MB
LONGBLOB高达 4GB
2.To store the uploaded image in the database you can do this:
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.To fetch and ouput the avatar you can do the following:
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)
));
});
So to access the avatar of the user with an id
equal to 10
you can just use this URL:
所以要与访问用户的头像id
等于10
你可以使用这个网址:
http://domain.com/user/10/avatar
回答by Yurich
You can store files in database in blob fields.
您可以将文件存储在数据库中的 blob 字段中。
Maybe this linkhelps 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}
并从 url 获取 img: /image/{img_id}