Search code examples
laravel-5intervention

Laravel - Convert Intervention Image Model(\Intervention\Image\Image) to UploadedFile model (\Illuminate\Http\UploadedFile)


The second parameter of the Storage::put method accept only \Illuminate\Http\UploadedFile(or File) model. How can i convert \Intervention\Image\Image to UploadedFile without save to storage?

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');
}

Solution

  • I think you will need to change your code to something like...

    $image_resize->encode() to $image_resize->stream()->__toString(); as i think the encode() would be redundant.

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

    Note that the filename is being set by the original name... i'd recommend changing that to a unique name, if you need help with that i'd start at https://laracasts.com/discuss/channels/general-discussion/how-to-generate-long-unique-name-for-filename?page=1