首页 > 解决方案 > 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]

标签: phplaravel

解决方案


您可以在将图像发送到服务器之前以 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;

}

推荐阅读