Here is how I tend to handle something like this so that it is reusable and easy to modify.
First rather than saving the file directly to the filesystem I make use of the Laravel Filesystem component.
I would add a filesystem disk for your uploads directory. If you open config/filesystems.php you can add the following to the disks array.
'uploads' => [
'driver' => 'local',
'root' => base_path() . '/public/uploads',
'visibility' => 'public'
],
This gives you the ability to move the files to cloud storage or use a cdn by ony changing some configuration and not having to touch your code.
The next thing I do is create a class that will handing the resizing and saving of the image file itself.
The class should receive Intervention\Image\ImageManager as a parameter in its constructor.
Then create a method in this class that receives Illuminate\Contracts\Filesystem\Filesystem, Symfony\Component\HttpFoundation\File\UploadedFile, and App\User (or whatever model the file is attached to) as parameters.
This method should determine the filename of the image, load the image from the uploaded file and resize it, then save it using the filesystem object.
namespace wherever\you\put\it;
use Intervention\Image\ImageManager;
use Illuminate\Contracts\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use App\User;
class ProfileImageSaver
{
private $image;
public function __construct(ImageManager $image)
{
$this->image = $image;
}
public function saveProfileImage(UploadedFile $file, Filesystem $filesystem, User $user)
{
$extension = $file->getClientOriginalExtension();
$filename = $user->id . '-' . time() . '.' . $extension;
$image = $this->image->make($file)->resize(260, 260)->encode($extension);
$path = "profile/{$filename}";
$filesystem->put($path, $image->getEncoded());
$image->destroy();
return $filename;
}
}
Your controller action would look something like this.
public function updateProfileImage(ProfileImageFormRequest $request, ProfileImageSaver $imageSaver)
{
$user = Auth::user();
$filename = $imageSaver->saveProfileImage($request->file('photo'), Storage::disk('uploads'), $user);
$user->profile_img = $filename;
$user->save();
return redirect('settings/details')->with('msg', 'Successfully updated profile image!');
}