"Cannot use Intervention\Image\Facades\Image as Image because the name is already in use"

Bobimaru

I am trying to figure out how to use Intervention Image library but I'm getting this error. I followed the Laravel integration on their website which had me do the following.

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.

Intervention\Image\ImageServiceProvider::class

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

'Image' => Intervention\Image\Facades\Image::class

I have a model called Image.php and I'm not sure if that matters.

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,
        Intervention\Image\ImageServiceProvider::class,

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

    ],

Aliases

'aliases' => [

        'App' => Illuminate\Support\Facades\App::class,
        'Artisan' => Illuminate\Support\Facades\Artisan::class,
        'Auth' => Illuminate\Support\Facades\Auth::class,
        'Blade' => Illuminate\Support\Facades\Blade::class,
        'Broadcast' => Illuminate\Support\Facades\Broadcast::class,
        'Bus' => Illuminate\Support\Facades\Bus::class,
        'Cache' => Illuminate\Support\Facades\Cache::class,
        'Config' => Illuminate\Support\Facades\Config::class,
        'Cookie' => Illuminate\Support\Facades\Cookie::class,
        'Crypt' => Illuminate\Support\Facades\Crypt::class,
        'DB' => Illuminate\Support\Facades\DB::class,
        'Eloquent' => Illuminate\Database\Eloquent\Model::class,
        'Event' => Illuminate\Support\Facades\Event::class,
        'File' => Illuminate\Support\Facades\File::class,
        'Gate' => Illuminate\Support\Facades\Gate::class,
        'Hash' => Illuminate\Support\Facades\Hash::class,
        'Lang' => Illuminate\Support\Facades\Lang::class,
        'Log' => Illuminate\Support\Facades\Log::class,
        'Mail' => Illuminate\Support\Facades\Mail::class,
        'Notification' => Illuminate\Support\Facades\Notification::class,
        'Password' => Illuminate\Support\Facades\Password::class,
        'Queue' => Illuminate\Support\Facades\Queue::class,
        'Redirect' => Illuminate\Support\Facades\Redirect::class,
        'Redis' => Illuminate\Support\Facades\Redis::class,
        'Request' => Illuminate\Support\Facades\Request::class,
        'Response' => Illuminate\Support\Facades\Response::class,
        'Route' => Illuminate\Support\Facades\Route::class,
        'Schema' => Illuminate\Support\Facades\Schema::class,
        'Session' => Illuminate\Support\Facades\Session::class,
        'Storage' => Illuminate\Support\Facades\Storage::class,
        'URL' => Illuminate\Support\Facades\URL::class,
        'Validator' => Illuminate\Support\Facades\Validator::class,
        'View' => Illuminate\Support\Facades\View::class,
        'Image' => Intervention\Image\Facades\Image::class

    ],

Image Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Image extends Model
{
    public function user(){
        return $this->belongsTo('App\User');
    }

    public function tags(){
        return $this->belongsToMany('App\Tag','tag_image','image_id','tag_id');
    }

    public function votes(){
        return $this->hasMany('App\Vote');
    }
}
Asur

This is probably an error caused by the auto-discovery feature introduced in Laravel 5.5.

Since the package is automatically registering the alias and you are registering manually it ends up being duplicated.

To solve it remove the service provider and the alias from the file and try to use it directly. You can check if the package is auto discovered when you execute a composer dumpautoload, it will generate an output like:

Generating optimized autoload files
Illuminate\Foundation\ComposerScripts::postAutoloadDump
@php artisan package:discover
Discovered Package: fideloper/proxy 
Discovered Package: laravel/tinker
Package manifest generated successfully.

It should show the intervention/image package discovered. You can check this behaviour implemented in their composer.json.

Now in order to use both classes at the same time you would need to assign a new alias to one of them like:

use Image as InterventionImage;
use App\Image;

Hope this helps you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does 11010100 << 1 equal 110101000, not 10101000?

Odd Java ternary behavior when assigning value. What is Java doing behind the scenes for this to happen?

Java 8 - How to get single values out of Map of Lists?

How to solve x - a tan(x) = 0 with Sympy

Spring @Transactional is consistantly rolling back transaction upon exception

Basic mySQL - Don't know how to show all results including 0

How can I refresh a page with jQuery?

C++ and Java array declaration/definition : differences

Spring security switch to Ldap authentication and database authorities

Multi language database, with default fallback

How to detect string which contains only spaces?

Change value in a 2-value column to its opposite only if a condition in another column is met

Protocol Buffer Error version

Move constructor C++11

qml virtual keyboard: keyboardDesignWidth and Height

Create a bot for a closed facebook group

align items vertically in rowlayout

eigen: Subtracting a scalar from a vector

Risk and effects of changing database collation in SQL Server

QML error on exposing child properties

clipping a voronoi diagram python

Resetting a QML StackView to initial state

function annotations in python

Monte Carlo Simulation Fails to Recognize Connected Graph

QML accessing your qml component

ERR_CONNECTION_TIMED_OUT with google apis fonts

Is searchsorted faster than get_loc to find label location in a DataFrame Index?

VLOOKUP within IMPORTRANGE

How to join two pandas Series into a single one with interleaved values?

Extract hyper-cubical blocks from a numpy array with unknown number of dimensions