2 回答

TA贡献1900条经验 获得超5个赞
我解决了!我像这样更改了控制器中的功能。
public function userLogout(Request $request)
{
$data = $request->all();
if ($request->isMethod('post')) {
if (preg_match('/^data:image\/(\w+);base64,/', $data['picture'])) {
$value = substr($data['picture'], strpos($data['picture'], ',') + 1);
$value = base64_decode($value);
$imageName = time() . '.png';
$val = Storage::disk('player-images')->put($imageName, $value);
$path = public_path('storage/player-images/'.$imageName);
Image::make($data['picture'])->resize(304, 277)->save($path);
$data['picture'] = 'player-images/' . $imageName;
User::where('name', Auth::user()->name)->update(['picture' => $data['picture']]);
}
}
return view('gallery');
}

TA贡献1804条经验 获得超7个赞
我知道您可能解决了您的疑问,但是我一直在寻找类似的东西但没有找到,因此我将以下代码留给可能需要它的人。此代码的目标是使用 URL 从 Pixabay上的图像。
我对类似问题的解决方案:
public function store(){
$url = json_decode(request('photo')); //Photo is the field that I fill in the view, that contain a URL to my image in pixabay;
$contents = file_get_contents($url);
$name = substr($url, strrpos($url, '/') + 1);
$blob = base64_encode($this->resizeImage($contents));
Photos::firstOrCreate(['photo' => $name,'thumb' => $blob]); //Photos is my model
}
private function resizeImage($contents) : string {
return (string) Image::make($contents)->resize(75, 75)->encode('data-url'); // Very important this cast to String, without it you can not save correctly the binary in your DB;
}
查看:为了在我的视图中使用代码:当我返回查看模型时,我使用“for”打印所有图像,如下所示:
@foreach($photos as $element)
<img src="{{ base64_decode($element->thumb) }}" alt="" >
@endforeach
#Warning 非常重要,您的数据库中有一个 Blob 字段,因此在 Laravel Migrate 的 Schema 中,您必须输入以下内容: $table->binary('thumb');
你可以在我的 git 上查看我的代码:lucasfranson10/multisafepay
- 2 回答
- 0 关注
- 188 浏览
添加回答
举报