Laravel如何保存base64数据图片?
Laravel how to save the base64 data image?
我知道如何使用 type="file"
保存来自 HTML <form>
的普通文件
也许我可以做到
$request->file('avatar')->store('avatars');
其实我用的是一个Js包关于一个image upload & drop & crop
HTML / Blade
<form action="{{route('avatar.update')}}" method="POST" enctype="multipart/form-data" id="avatar">
<div class="slim rounded-2xl "
data-label="Select your Avatar"
data-size="400,400"
data-ratio="1:1">
<input type="file" name="avatar" >
</div>
<button type="submit"> Update </button>
</form>
当表单提交时选择了一张图片。
$request->file('avatar')
= 空
因为我在提交的时候检查,input('avatar')
会变成type="hidden"
,所以我可以用$request->file('avatar')
那么……
dd($request->all());
我得到了
[
"_token" => "od91HbEuWSbgx5gqfaNOeHQIC8qUBMumzf7u4Dvr",
"avatar" => "{
"server":null,
"meta":{},
"input":{"name":"ccc.png","type":"image/png","size":1412200,"width":2860,"height":1440,"field":null},
"output":{"name":"ccc.png",
"type":"image/png",
"width":400,"height":400,
"image":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUCC....etc"},
"actions":{"rotation":null,"crop":{"x":710,"y":0,"height":1440,"width":1440,"type":"auto"},"size":{"width":400,"height":400},"filters":{"sharpen":0},"minSize":{"width":0,"height":0}}
}"
]
我得到的这张图片是base64格式的?我怎样才能保存它?
我尝试 base64_decode
但没有用。
$avatar = base64_decode($request->input('avatar'));
解决了来自@gguney 的问题!
主要问题是忘记json_decode头像输入了!
控制器代码现在
$image = json_decode($request->input('avatar'))->output->image;
$imageName = time() . 'ava.png';
$image = str_replace('data:image/png;base64,', '', $image);
$image = str_replace(' ', '+', $image);
Storage::disk('temp_avatar')->put($imageName, base64_decode($image));
这应该有效:
json_decode($request->input('avatar'))->output->image
我知道如何使用 type="file"
HTML <form>
的普通文件
也许我可以做到
$request->file('avatar')->store('avatars');
其实我用的是一个Js包关于一个image upload & drop & crop
HTML / Blade
<form action="{{route('avatar.update')}}" method="POST" enctype="multipart/form-data" id="avatar">
<div class="slim rounded-2xl "
data-label="Select your Avatar"
data-size="400,400"
data-ratio="1:1">
<input type="file" name="avatar" >
</div>
<button type="submit"> Update </button>
</form>
当表单提交时选择了一张图片。
$request->file('avatar')
= 空
因为我在提交的时候检查,input('avatar')
会变成type="hidden"
,所以我可以用$request->file('avatar')
那么……
dd($request->all());
我得到了
[
"_token" => "od91HbEuWSbgx5gqfaNOeHQIC8qUBMumzf7u4Dvr",
"avatar" => "{
"server":null,
"meta":{},
"input":{"name":"ccc.png","type":"image/png","size":1412200,"width":2860,"height":1440,"field":null},
"output":{"name":"ccc.png",
"type":"image/png",
"width":400,"height":400,
"image":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUCC....etc"},
"actions":{"rotation":null,"crop":{"x":710,"y":0,"height":1440,"width":1440,"type":"auto"},"size":{"width":400,"height":400},"filters":{"sharpen":0},"minSize":{"width":0,"height":0}}
}"
]
我得到的这张图片是base64格式的?我怎样才能保存它?
我尝试 base64_decode
但没有用。
$avatar = base64_decode($request->input('avatar'));
解决了来自@gguney 的问题!
主要问题是忘记json_decode头像输入了!
控制器代码现在
$image = json_decode($request->input('avatar'))->output->image;
$imageName = time() . 'ava.png';
$image = str_replace('data:image/png;base64,', '', $image);
$image = str_replace(' ', '+', $image);
Storage::disk('temp_avatar')->put($imageName, base64_decode($image));
这应该有效:
json_decode($request->input('avatar'))->output->image