保存值 Base64 到 Image PNG use Intervention image Laravel 存储
Save value Base64 to Image PNG use Intervention image Laravel Storage
如何使用干预图像和 laravel 存储将 Base64 值转换为图像 PNG?
public function userLogout(Request $request) {
$data = $request->all();
if ($request->isMethod('post')) {
$poster = explode(";base64", $request->picture);
$image_type = explode("image/", $poster[0]);
$mime_type = '.'.$image_type[1];
$image_base = base64_decode($poster[1]);
$data['picture'] = Storage::disk('player-images')->put($image_base, file_get_contents($poster));
$path = public_path('storage/player-images/'.$image_base);
Image::make($poster)->save($path);
$data['picture'] = 'player-images/' . $image_base;
User::where('name', Auth::user()->name)->update($data);
}
return view('gallery');
}
我收到一条错误消息:
"file_get_contents() expects parameter 1 to be a valid path, array given"
这是我的 ajax 函数
var canvas = document.getElementById('canvas');
var dataUrl = canvas.toDataURL('image/png');
$(document).ready(function(){
$('#save').click(function(e){
e.preventDefault();
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
type: "POST",
url: "/gallery",
data: {
picture: dataUrl,
}
}).done(function(o) {
console.log("saved");
});
});
});
如何将 base64 值保存到数据库,如 player-images/blabla.png 并将图像存储到路径 public/storage/player-images/
对不起,我的英语不好。
谢谢。
我解决了!我像这样更改了控制器中的功能。
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');
}
我知道你可能解决了你的疑问,但我一直在寻找类似的东西但没有找到,所以我将把下面的代码留给可能需要它的人。
此代码的目标是使用 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;
}
查看:
为了在我看来使用代码,我输入了:
当我 return 查看模型时,我使用 'for' 打印所有图像,如下所示:
@foreach($photos as $element)
<img src="{{ base64_decode($element->thumb) }}" alt="" >
@endforeach
#警告
您的数据库中有一个 Blob 字段非常重要,因此在 Laravel 迁移的架构中,您必须将内容设置为:$table->binary('thumb');
您可以在我的 git 上查看我的代码:
lucasfranson10/multisafepay
如何使用干预图像和 laravel 存储将 Base64 值转换为图像 PNG?
public function userLogout(Request $request) {
$data = $request->all();
if ($request->isMethod('post')) {
$poster = explode(";base64", $request->picture);
$image_type = explode("image/", $poster[0]);
$mime_type = '.'.$image_type[1];
$image_base = base64_decode($poster[1]);
$data['picture'] = Storage::disk('player-images')->put($image_base, file_get_contents($poster));
$path = public_path('storage/player-images/'.$image_base);
Image::make($poster)->save($path);
$data['picture'] = 'player-images/' . $image_base;
User::where('name', Auth::user()->name)->update($data);
}
return view('gallery');
}
我收到一条错误消息:
"file_get_contents() expects parameter 1 to be a valid path, array given"
这是我的 ajax 函数
var canvas = document.getElementById('canvas');
var dataUrl = canvas.toDataURL('image/png');
$(document).ready(function(){
$('#save').click(function(e){
e.preventDefault();
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
type: "POST",
url: "/gallery",
data: {
picture: dataUrl,
}
}).done(function(o) {
console.log("saved");
});
});
});
如何将 base64 值保存到数据库,如 player-images/blabla.png 并将图像存储到路径 public/storage/player-images/
对不起,我的英语不好。 谢谢。
我解决了!我像这样更改了控制器中的功能。
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');
}
我知道你可能解决了你的疑问,但我一直在寻找类似的东西但没有找到,所以我将把下面的代码留给可能需要它的人。 此代码的目标是使用 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;
}
查看: 为了在我看来使用代码,我输入了: 当我 return 查看模型时,我使用 'for' 打印所有图像,如下所示:
@foreach($photos as $element)
<img src="{{ base64_decode($element->thumb) }}" alt="" >
@endforeach
#警告 您的数据库中有一个 Blob 字段非常重要,因此在 Laravel 迁移的架构中,您必须将内容设置为:$table->binary('thumb');
您可以在我的 git 上查看我的代码: lucasfranson10/multisafepay