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

how can compress the uploaded image, convert into base 64 and directly store in database using laravel

我想对上传的图片进行压缩,并将图片的base64存储到数据库中。每次我 google 这个问题我总是想到使用存储在特定路径中的图像。我正在使用 intervention/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格式后存入数据库。 Error that I got

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

希望有用。

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