Question

Magento 2.2.1 with custom theme which inherits from Magento/blank.

Attempting to fix the JPG compression for product images.

I know, based on this solution, that I must extend the Image class found here:

Magento_Catalog/Model/Product/Image.php

I now understand the basic file structure properly, thanks to Quan Le's answer.

I have created the following structure:

+-- app
    +-- code
        +-- [Vendor]
            +-- module-image-fix
                +-- registration.php
                +-- etc
                    +-- di.xml
                    +-- module.xml
                +-- Model
                    +-- Product
                        +-- Image.php

I know that there is a problem here, but I can't work out how to properly label directories within the files themselves. The solution I am working from does not clearly label the files or file structure, so I'm not sure if I even need registration.php etc.

I have tried renaming module-image-fix to ImageFix with no luck.

registration.php

<?php
  \Magento\Framework\Component\ComponentRegistrar::register(
  \Magento\Framework\Component\ComponentRegistrar::MODULE,
  '[Vendor]_ImageFix',
  __DIR__
);

di.xml :

<?xml version="1.0"?>
  <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Model\Product" type="[Vendor]\ImgFix\Model\Product" />
  </config>

module.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="[Vendor]_ImageFix" setup_version="1.0.1">
        </module>
    </config>

Image.php

<?php
    namespace [Vendor]\ImageFix\Model\Product;
    class Image extends \Magento\Catalog\Model\Product\Image {
        protected function _construct() {
            $this->_quality = 100;
            return parent::_construct();
        }
    }

I have so far managed to successfully enable the module, but without any change to images.

I have also managed to enable the module, but received an error that [Vendor]/ImageFix/Models/Product does not exist.

Was it helpful?

Solution

Just keep your custom module in app/code folder follow Magento 2 Module Structure like this

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top