php 使用 Laravel 将图像转换为 base 64 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46206992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert image to base 64 string with Laravel
提问by Pondikpa Tchabao
I want to convert an image to base 64 with Laravel. I get the image from a form . I tried this in my controller:
我想使用 Laravel 将图像转换为 base 64。我从表单中获取图像。我在我的控制器中试过这个:
public function newEvent(Request $request){
$parametre =$request->all();
if ($request->hasFile('image')) {
if($request->file('image')->isValid()) {
try {
$file = $request->file('image');
$image = base64_encode($file);
echo $image;
} catch (FileNotFoundException $e) {
echo "catch";
}
}
}
I get this only:
我只得到这个:
L3RtcC9waHBya0NqQlQ=
L3RtcC9waHBya0NqQlQ=
回答by Magnus Eriksson
Laravel's $request->file()
doesn't return the actual file content. It returns an instance of the UploadedFile
-class.
Laravel$request->file()
不会返回实际的文件内容。它返回UploadedFile
-class 的一个实例。
You need to load the actual file to be able to convert it:
您需要加载实际文件才能对其进行转换:
$image = base64_encode(file_get_contents($request->file('image')->pat??h()));
回答by C47
It worked for me in the following way:
它以以下方式对我有用:
$image = base64_encode(file_get_contents($request->file('image')));
I eliminated this part ->pat??h();
我删除了这部分 ->pat??h();
回答by Rick Holtman
$image = base64_encode(file_get_contents($request->file('image')));
$image = base64_encode(file_get_contents($request->file('image')));
Works also for me.
也适用于我。