0

I have installed this package https://github.com/Intervention/image with composer. I have added

'IntImage' => 'Intervention\Image\Facades\Image'

to config/app under aliases

I get the following error and cant figure out what I am doing incorrectly I am sure it has something to do with namespacing /autoloading but app/acme is in the psr-o section of composer.json

'Argument 1 passed to Acme\Services\Images\InterventionImageEditor::__construct() must be an instance of IntImage, none given, called in /var/www/app/ACme/Providers/ImageEditorServiceProvider.php on line 14 and defined' in /var/www/app/Acme/Services/Images/InterventionImageEditor.php:11

I have the following directory structure

app
  acme
    Providers
      ImageEditorServiceProvider.php
    Services
      Images
        ImageEditorInterface.php
        InterventionImageEditor.php

and the content of the files

ImageEditorServiceProvider.php

<?php namespace Acme\Providers;

use Illuminate\Support\ServiceProvider;
use Acme\Services\Images\InterventionImageEditor;

/**
*
*/
class ImageEditorServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind('Acme\Services\Images\ImageEditorInterface', function () {
            return new InterventionImageEditor();
        });
    }
}

ImageEditorInterface.php

<?php namespace Acme\Services\Images;

interface ImageEditorInterface
{
    public function hello();
}

InterventionImageEditor.php

<?php namespace Acme\Services\Images;

use IntImage;

/**
*
*/
class InterventionImageEditor implements ImageEditorInterface
{
    protected $imageeditor;

    public function __construct(IntImage $imageeditor)
    {
        $this->imageeditor = $imageeditor;
    }

    public function hello()
    {
        $hello = 'hello';

        return $hello;
    }
}

Can I

Use IntImage;

in this way because it is a facade or am I missing something?

edit to include solution;

changing the service provider to the following resolved the problem

<?php namespace Acme\Providers;

use Illuminate\Support\ServiceProvider;
use Acme\Services\Images\InterventionImageEditor;
use IntImage;

/**
*
*/
class ImageEditorServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind('Acme\Services\Images\ImageEditorInterface', function () {
            $intimage = new IntImage;
            return new InterventionImageEditor($intimage);
        });
    }
}

2 Answers 2

1

The error is coming from ImageEditorServiceProder.php:

$this->app->bind('Acme\Services\Images\ImageEditorInterface', function () {
  return new InterventionImageEditor();
});

Here you are instantiating the InterventionImageEditor without any parameters. You InterventionImageEditor requires one parameter of type IntImage.

If there are places where you won't have IntImage when instantiating InterventionImageEditor then you need to update your InterventionImageEditor::__construct so that it accepts null(possibly).

function __construct(IntImage $imageeditor = null)
{
    if (is_null($imageeditor)) {
        // Construct a default imageeditor
        // $imageeditor = new ...;
    }

    $this->imageeditor = $imageeditor;
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the reply. IntImage is an instance of the intervention/image class sp InterventionImageEditor will always need it. However why wont InterventionImageEditor get IntImage itself from the 'Use IntImage;' code at the top of the file?
Seems like I have to pass IntImage from the Service Provider and then reference it in InterventionImageEditor as well. I thought I only had to do it in the latter. If you have the time to explain to me why this is as an edit in your answer I would appreciate. If not thanks for heling anyway
1

i am not sure you can using IntImage because this file is Facades.

if you want to extending the intervention class. you should add Intervention\Image\Image to your ImageEditorServiceProvider.

use Intervention\Image\Image;

class ImageEditorServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind('Acme\Services\Images\ImageEditorInterface', function () {

            return new InterventionImageEditor(new Image);
        });
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.