干预编码后将文件保存到存储

时间:2017-01-22 14:53:56

标签: laravel laravel-5.3 intervention

我试图将Intervention图像编码保存到特定存储空间

$converted_image = Image::make($req->file('image_file'));
$converted_image->resize(300, null, function ($constraint) {
    $constraint->aspectRatio();
});
$file=$converted_image->encode('png');
$path = Storage::putFileAs('images', $file->stream(), 'test.png');

返回错误:

Call to undefined method GuzzleHttp\Psr7\Stream::getRealPath()

另一次尝试:

$path = Storage::putFileAs('images', $file->stream()->__toString(), 'test.png');

错误:

Call to a member function getRealPath() on string

使用存储保存文件的任何解决方法?

2 个答案:

答案 0 :(得分:1)

问题是Storage::putFileAs()需要\Illuminate\Http\File\Illuminate\Http\UploadedFile,这就是它尝试在其上调用getRealPath()的原因。

然而,Storage::put没有,它会很乐意接受PSR-7流。尝试:

Storage::put('images/test.png', $file->stream());

答案 1 :(得分:-1)

建立在多米尼克的答案上:

您需要File或UploadedFile的实例。

例如:

Storage::putFileAs('images', new Illuminate\Http\File($file->stream()->__toString()), 'test.png');