如何使用laravel压缩上传的图像,转换成base 64并直接存储在数据库中

如何使用laravel压缩上传的图像,转换成base 64并直接存储在数据库中,laravel,base64,compression,Laravel,Base64,Compression,我想压缩上传的图像,并将图像的base64存储在数据库中。每次我用谷歌搜索这个问题时,我总是想到使用存储在特定路径中的图像。我正在使用干预/image软件包 我试过这个密码 $img = Image::make($request->photo)->resize(300, 200); //get the extension $type = $request->file('photo')->getClientOriginalExtension(); //convert it

我想压缩上传的图像,并将图像的base64存储在数据库中。每次我用谷歌搜索这个问题时,我总是想到使用存储在特定路径中的图像。我正在使用
干预/image
软件包

我试过这个密码

$img = Image::make($request->photo)->resize(300, 200);
//get the extension
$type = $request->file('photo')->getClientOriginalExtension();
//convert it into the base64.
$base64 = 'data:image/' . $type . ';base64,' . base64_encode(file_get_contents($img));

我想在压缩并转换成base64格式后将照片存储在数据库中

无需编写任何压缩或转换代码。 尝试添加
encode('data-url')

我希望它能起作用

$img = Image::make($request->photo)->resize(300, 200)->encode('data-url');

再次检查内容是否100%不同,并删除标志。出现错误
$img为空
,您在哪里填写了为
文件获取内容()提供的
$img
<代码>base64编码(文件获取内容($img))
$img位于代码部分的第一行。
file\u get\u content()