0

I'm using laravel 5.8 and intervention image to upload and resize an image.

I want to store this in storage/app/public/images folder and I'm trying to use the storage facade to auto generate a unique name but the following doesn't work:

 $file = $request->file('file');

 $image = Image::make($file->getRealPath())->resize(360, 180);
 Storage::disk('public')->putFile('images', $image->getEncoded());

Is it possible to auto generate unique file name using the storage facade for images similar to the when you upload normal files as follows:

Storage::disk('local')->putFile('forms', $request->file('file'));
thisiskelvin
  • 4,136
  • 1
  • 10
  • 17
adam78
  • 9,668
  • 24
  • 96
  • 207

2 Answers2

0

Uploaded files use temporary file names when uploaded in your application. These can serve as unique names you can use to save.

You can access this using the ->getPathName() method of the uploaded file.

Alternatively, you can use ->getClientOriginalName() for the original name of the file.

With Intervention/Image, you can use the ->save() method to save this image to the destination folder:

$file = $request->file('file');

$image = Image::make($file->getRealPath())
    ->resize(360, 180)
    ->save(storage_path('app/public/images/'.$file->getPathName());

I haven't tested this but I hope this helps.

thisiskelvin
  • 4,136
  • 1
  • 10
  • 17
0

$image = Image::make($request->file('file'))->resize(360, 180) ->save(storage_path('app/public/images/'.$file->getPathName());

  • 1
    While this code snippet may solve the question, [including an explanation](//meta.stackoverflow.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Filnor Apr 30 '20 at 13:21