我必须从数据库中存储和检索图像。我想存储一个图像,而不是图像路径或图像名称。当我搜索它时,我只找到了存储图像路径或名称的解决方案。那么,有人能告诉我如何从数据库中存储和检索大小为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发布于 2016-02-16 19:47:35
实际上,对于Laravel,它只涉及几行代码。假设您有一个用户,其头像存储在数据库中。假设您使用的是MySQL,下面是如何从数据库中存储和检索化身的方法:
1.首先,您需要在users表中有一个可以存储二进制数据的avatar列。根据您希望头像图像的大小,列的数据类型可以是以下类型之一:
最大64KB的
BLOB
高达16MB的MEDIUMBLOB
LONGBLOB高达4 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)
));
});因此,要使用等于10的id访问用户的头像,只需使用以下网址:
http://domain.com/user/10/avatar发布于 2016-02-16 18:29:06
您可以将文件存储在数据库的blob字段中。
也许this link能帮到你
然后你可以创建指向你的图像的url:
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
https://stackoverflow.com/questions/35428876
复制相似问题