将图像流保存到 Laravel

在我的 Laravel 控制器中,我试图获取一串 png 图像数据,然后使用以下命令将其保存为png存储put:


$image_string = $request->get('image_string', false);

$filename = 'temp_image';


if ($image_string) {

    $resource = imagecreatefromstring($image_string);

    Log::debug("Storing image");

    if ($resource !== false) {

        Storage::put('public/' . $filename . '.png', imagepng($resource));

    } else {

        Log::error("Failed to get resource from string");

    }

}

如果我只拆分imagecreatefromstring($image_string)和imagepng($resource)部分,则会按预期创建文件。但Storage::put图像中的某处要么损坏,要么丢失,因为虽然存在具有正确文件名和扩展名的图像,但它没有数据,并且在查看时显示为黑框。


我需要以不同的方式处理图像存储例程吗?


翻过高山走不出你
浏览 172回答 2
2回答

元芳怎么了

解决方案融合了这两种想法。$image_string = $request->get('image_string', false);$filename = 'temp_image';if ($image_string) {    $resource = Image::make($image_string)->encode('png');    Log::debug("Storing image");    if ($resource !== false) {        Storage::put("public/{$filename}.png", (string) $resource);    } else {       Log::error("Failed to get resource from string");    }}
打开App,查看更多内容
随时随地看视频慕课网APP