Zend Framework 3. Module (ZfImageResizer) could not be initialized

I have the problem :frowning:
If I just add in config/autoload/global.php:

  'service_manager' => [
      'factories' => [
         ...
         ImageResizerService::class => ImageResizerServiceFactory::class,
         ImageMagick::class  => ImageMagickFactory::class,
      ],
   ],

and

   'rvdlee' => [
      'zf-image-resizer'   => [
         'adapter'         => \Rvdlee\ZfImageResizer\Adapter\ImageMagick::class,
         'validator-chain' => [
            ['name' => \Zend\Validator\File\IsImage::class],
         ],
      ],
   ],

Than I can get ImageResizerService in any factory by:

   public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
   {
         ...
         $imageResizerService = $container->get(ImageResizerService::class);
         ...
   }

As in extension’s documentation GitHub - rvdlee/zf-image-resizer: A Zend Package to make image resizing easier..
But as I understood, this must working without providing of factories in configuration. As I see, extension have all this configuration (and more) in vendor/rvdlee/zf-image-resizer/config/module.config.php:

return [
    'console'         => [
        'router' => [
            'routes' => [
                'console_image_resizer' => [
                    'options' => [
                        'route'    => 'image-resizer [--image=]',
                        'defaults' => [
                            'controller' => ConsoleController::class,
                            'action'     => 'resizeImage',
                        ],
                    ],
                ],
            ],
        ],
    ],
    'controllers'     => [
        'factories' => [
            ConsoleController::class => ConsoleControllerFactory::class,
        ],
    ],
    'service_manager' => [
        'factories' => [
            ImageResizerService::class => ImageResizerServiceFactory::class,

            ImageMagick::class  => ImageMagickFactory::class,
        ],
    ],
    'filters'   => [
        'factories' => [
            ImageResizer::class => ImageResizerFactory::class,
        ],
    ],
];

This should be handled when loading the module. But if I trying to add modulename (‘ZfImageResizer’) to config/modules.config.php of my Application module, than I get the fatal error:

Fatal error: Uncaught Zend\ModuleManager\Exception\RuntimeException: Module (ZfImageResizer) could not be initialized. in Path/To/My/Project\vendor\zendframework\zend-modulemanager\src\ModuleManager.php:201

Stack trace: #0 Path/To/My/Project\vendor\zendframework\zend-modulemanager\src\ModuleManager.php(173): Zend\ModuleManager\ModuleManager->loadModuleByName(Object(Zend\ModuleManager\ModuleEvent))
#1 Path/To/My/Project\vendor\zendframework\zend-modulemanager\src\ModuleManager.php(95): Zend\ModuleManager\ModuleManager->loadModule(‘ZfImageResizer’)

#2 Path/To/My/Project\vendor\zendframework\zend-eventmanager\src\EventManager.php(322): Zend\ModuleManager\ModuleManager->onLoadModules(Object(Zend\ModuleManager\ModuleEvent))

#3 Path/To/My/Project\vendor\zendframework\zend-eventmanager\src\EventManager.php(171): Zend\EventManager\EventManager->triggerListeners(Object(Zend\ModuleManager\Modul in Path/To/My/Project\vendor\zendframework\zend-modulemanager\src\ModuleManager.php on line 201

Please, tell me, where is my mistake can be? Thank you!

Hello and welcome to our forums! :smiley:

To use the module, you must do the following:

  1. Install the module via Composer
  2. Add the module to config/modules.config.php with the name Rvdlee\ZfImageResizer
  3. Extend the application configuration, e.g. config/autoload/global.php:
    return [
        'rvdlee' => [
            'zf-image-resizer' => [
                'adapter'         => Rvdlee\ZfImageResizer\Adapter\ImageMagick::class,
                'validator-chain' => [
                    ['name' => Zend\Validator\File\IsImage::class],
                ],
            ],
        ],
    ];
    

Then you can use the filter in your forms or the service in a controller via:

namespace Application\Controller;

use Rvdlee\ZfImageResizer\Service\ImageResizerService;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    private ImageResizerService $imageResizerService;

    public function __construct(ImageResizerService $imageResizerService)
    {
        $this->imageResizerService = $imageResizerService;
    }

    public function indexAction()
    {
        $result = $this->imageResizerService->resizeImage(
            '/path/to/image/example.jpg',
            150
        );
        var_dump($result);
        var_dump($this->imageResizerService->getLogger()->getWriters()->toArray());

        return new ViewModel();
    }
}

Extend the controller configuration:

'controllers' => [
    'factories' => [
        \Application\Controller\IndexController::class => \Zend\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory::class,
    ],
],