Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将值Base64保存到图像PNG使用干预图像Laravel存储_Php_Laravel_Intervention - Fatal编程技术网

Php 将值Base64保存到图像PNG使用干预图像Laravel存储

Php 将值Base64保存到图像PNG使用干预图像Laravel存储,php,laravel,intervention,Php,Laravel,Intervention,如何使用干预图像和laravel存储将Base64值转换为图像PNG 公共功能用户注销(请求$Request){ $data=$request->all(); 如果($request->isMethod('post')){ $poster=explode(“;base64”,$request->picture); $image_type=explode(“image/”,$poster[0]); $mime_类型='。$image_类型[1]; $image_base=base64_decode

如何使用干预图像和laravel存储将Base64值转换为图像PNG

公共功能用户注销(请求$Request){
$data=$request->all();
如果($request->isMethod('post')){
$poster=explode(“;base64”,$request->picture);
$image_type=explode(“image/”,$poster[0]);
$mime_类型='。$image_类型[1];
$image_base=base64_decode($poster[1]);
$data['picture']=Storage::disk('player-images')->put($image\u base,file\u get\u contents($poster));
$path=public_path('storage/player images/'。$image_base);
图像::制作($poster)->保存($path);
$data['picture']='player images/'。$image\u base;
User::where('name',Auth::User()->name)->update($data);
}
返回视图(“画廊”);
}
我收到一条错误消息:

文件\u get\u contents()要求参数1为有效路径,数组给定

这是我的ajax函数

var canvas=document.getElementById('canvas');
var dataUrl=canvas.toDataURL('image/png');
$(文档).ready(函数(){
$(“#保存”)。单击(函数(e){
e、 预防默认值();
$.ajax({
标题:{'X-CSRF-TOKEN':$('meta[name=“CSRF_TOKEN”]”)。attr('content')},
类型:“POST”,
网址:“/gallery”,
数据:{
图片:dataUrl,
}
}).完成(功能(o){
控制台日志(“已保存”);
});
});
});
如何将base64值保存到player images/blabla.png等数据库中,并将图像存储到路径public/storage/player images中/

对不起,我的英语不好。
谢谢。

我解决了它!。我像这样改变了控制器的功能

公共功能用户注销(请求$Request)
{
$data=$request->all();
如果($request->isMethod('post')){
if(preg_匹配('/^data:image\/(\w+);base64,/',$data['picture'])){
$value=substr($data['picture']、strpos($data['picture']、'、')+1);
$value=base64_解码($value);
$imageName=time().'.png';
$val=Storage::disk('player-images')->put($imageName,$value);
$path=public_path('storage/player images/'。$imageName);
图像::make($data['picture'])->调整大小(304277)->保存($path);
$data['picture']='player images/'。$imageName;
User::where('name',Auth::User()->name)->update(['picture'=>$data['picture']]);
}
}
返回视图(“画廊”);
}

我知道您可能已经解决了您的疑问,但我正在寻找类似的东西,但没有找到,因此我将为可能需要它的人留下以下代码。 此代码的目标是使用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
@foreach($photos作为$element)
拇指)}}“alt=”“>
@endforeach
#警告 在数据库中有一个Blob字段是非常重要的,因此在Laravel Migrate的模式中,您必须放置一些内容:$table->binary('thumb')

您可以在我的git上查看我的代码:
lucasfranson10/multisafepay

在数据库中存储
base64()
数据并将文件保存到文件夹时是否冗余?我将数据类型更改为text。但我希望它存储在数据库中,而不是base64,而是像播放器图像/(值)一样的链接存储