Search code examples
laravellaravel-5intervention

Installed Intervention with Laravel but resize method does not exist


I am using Intervention with Laravel 5.2, I have installed it using Composer and included Intervention\Image\ImageServiceProvider::class and 'Image' => Intervention\Image\Facades\Image::class in the config/app.php

I have also added the use statement to the Controller where I am using it use Intervention\Image\ImageManager;

Here is my function where I am trying to process the photo, but when I submit the form that calls this function I get this error message

 BadMethodCallException in Macroable.php line 81:
    Method resize does not exist.

Function

public function postAvatarUpload(Request $request)
    {
         $this->validate($request, [
            'image' => 'required|image|max:3000|mimes:jpeg,jpg,png',
        ]);
        $user = Auth::user();
        $usersname = $user->username;
        $file = $request->file('image');
        $resizedImg = $file->resize(200,200);
        $ext = $file->getClientOriginalExtension();
        $filename = $usersname . '.' . $ext;
        if (Storage::disk('public')->has($usersname)) {
            Storage::delete($usersname);
        }
           Storage::disk('public')->put($filename, File::get($resizedImg));
           $avatarPath = Storage::url($filename);
            Auth::user()->update([
                'image' => $avatarPath,
            ]);

        return redirect()->route('profile.index', 
                ['username' => Auth::user()->username]);
    }

Solution

  • First you should save the file and then create instance (object) of ImageManager using the method make.

    Example:

    public function upload(Request $request)
    {
        $file = $request->file('image');
    
        $path = 'path/to';
        $fileName = 'example_name.' . $file->extension();
    
        $file->move($path, $fileName);
    
        $image = ImageManager::make($path . DIRECTORY_SEPARATOR . $fileName);
    }
    

    Also, you may use the facade Intervention\Image\Facades\Image instead of ImageManager class.