laravel 5.6 image intervention upload with rename and resize

Step 1-Installation:
(composer require intervention/image)

Step 2-Configuration:
After you have installed Intervention Image, open your Laravel config file config/app.php and add the following lines.

In the $providers array add the service providers for this package.
InterventionImageImageServiceProvider::class

Add the facade of this package to the $aliases array.

‘Image’ => InterventionImageFacadesImage::class

Step 3-Uses:
use Image

 public function avatar(Request $request){
    $user = new User();
    if($request->hasFile('image')) {
        if ($user->image){
            unlink(public_path('/image/user/').$user->image);
        }
        $image = $request->file('image');
        $imageName = $image->getClientOriginalName();
        $fileName = $userName . "_profile_". $userId . "_" . $imageName;

        $directory = public_path('/image/user/');
        $imageUrl = $directory.$fileName;
        Image::make($image)->resize(200, 200)->save($imageUrl);
        $user->image = $fileName;
    }

    if ($user->save())
        return redirect()->back()->with('success','Update successfully');

    return redirect()->back()->with('error', 'There is an error message');
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

step 2 is not needed if you are working on laravel 5.6 composer require intervention/image will install in your vendor folder and laravel package discovery will do the rest for you


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x