php - Laravel 通过 API 上传图片
问题描述
我正在尝试通过 API 上传图像,但它一直在我的日志中抛出此错误,并且图像没有上传。
[2020-03-06 16:27:46] local.ERROR:此 PHP 安装不支持读取 Exif 数据。{"userId":10002,"exception":"[object] (Intervention\Image\Exception\NotSupportedException(code: 0): 此 PHP 安装不支持读取 Exif 数据。在 /home/zackdemo/public_html/app/ vendor/intervention/image/src/Intervention/Image/Commands/ExifCommand.php:22) [stacktrace]
解决方案
您可以在将图像发送到服务器之前以 base64 格式对图像进行编码。PHP 具有解码 base 64 格式的工具。
您可以执行以下操作。
public static function storeBase64($base64Image, $extension, $participant)
{
$oldImage = $participant->image;
$image = $base64Image; // your base64 encoded
$image = str_replace('data:image/' . $extension . ';base64,', '', $image);
$image = str_replace(' ', '+', $image);
$imageName = str_random(10) . "-" . $participant->id . '.' . $extension;
$path = 'storage/participant/profile/' . $imageName;
Storage::put($path, base64_decode($image));
$participant->image = $path;
$participant->save();
if ($oldImage) {
// Storage::delete($oldImage);
Storage::move($oldImage, 'deleted/' . $oldImage );
}
return $path;
}
推荐阅读
- docker - 特定的 `build` 目录未添加到 Docker 映像中
- javascript - 使用 JS 或其他方法点击后更改嵌入式 youtube 播放器的不透明度
- javascript - Tablesorter如何从表中删除选定的行
- c# - Google Drive API - 使用 API 密钥请求文件列表时出现权限错误 403 - C#
- css - 在 Stenciljs 中单击时更改 CSS 类
- powershell - PowerShell:如何从 .appx 包路径获取身份信息?
- c++ - TCL 无法识别 C++ 命令 NS2
- python - 在python上修改全局变量传入参数函数
- go - 是否可以在 prisma 中定义接口?
- java - java.lang.IllegalArgumentException:SQL 数组不能为空