Intervention Image - command is not available

Intervention Image - command is not available

Jan 8, 2017,

If you're receiving similar errors you may be confused.

Command (getRealPath) is not available for driver (Gd)
Command (hashName) is not available for driver (Gd)
Command (hashName) is not available for driver (Imagick)

So, what is going wrong?

It is easy, most likely you're passing Intervention\Image\Image as an object to a method that expects another class.

Ok, why we're receiving these errors?

Intervention Image package uses a magic __call methods and Intervention\Image\AbstractDriver is trying to find a proper class.

    /**
     * Returns classname of given command name
     *
     * @param  string $name
     * @return string
     */
    private function getCommandClassName($name)
    {
        $drivername = $this->getDriverName();
        $classnameLocal = sprintf('\Intervention\Image\%s\Commands\%sCommand', $drivername, ucfirst($name));
        $classnameGlobal = sprintf('\Intervention\Image\Commands\%sCommand', ucfirst($name));

        if (class_exists($classnameLocal)) {
            return $classnameLocal;
        } elseif (class_exists($classnameGlobal)) {
            return $classnameGlobal;
        }

        throw new \Intervention\Image\Exception\NotSupportedException(
            "Command ({$name}) is not available for driver ({$drivername})."
        );
    }

And now real example from stackoverflow:

$image = Image::make($path)->fit(300)->encode('jpg');
$store = Storage::putFile('public/image', $image); // <=== ERROR HERE

Storage::putFile expects \Illuminate\Http\UploadedFile or \Illuminate\Http\File but got Intervention\Image\Image.

Now when we understand a reason, it is easy to find a proper method:

$image = Image::make($path)->fit(300)->encode('jpg');
$store = Storage::put('public/image/myCustomName.jpg', $image->__toString());