Laravel - 将干预图像模型(\ Intervention \ Image \ Image)转换为UploadedFile模型(\ Illuminate \ Http \ UploadedFile)

时间:2017-07-11 18:14:50

标签: laravel-5 intervention

Storage :: put方法的第二个参数只接受\ Illuminate \ Http \ UploadedFile(或File)模型。如何将\ Intervention \ Image \ Image转换为UploadedFile而不保存到存储?

if($request->hasFile('image')) {
  $image = $request->file('image');
  $image_resize = Image::make($image->getRealPath());
  $image_resize->resize(300, 300);
  $image_resize->encode();
  $imagePath = Storage::put(public_path(), new File($image_resize), 'public');
}

1 个答案:

答案 0 :(得分:1)

我认为您需要将代码更改为......

$image_resize->encode()$image_resize->stream()->__toString();因为我认为encode()会是多余的。

然后Storage::put(public_path(), new File($image_resize), 'public')Storage::disk('local')->put($image->getClientOriginalName(), $image->stream()->__toString(), 'public');

注意文件名由原始名称设置...我建议将其更改为唯一名称,如果您需要帮助,我可以从https://laracasts.com/discuss/channels/general-discussion/how-to-generate-long-unique-name-for-filename?page=1