0

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.

  • I made a module to make it easier to edit this value in the admin. See github.com/superdav42/magento-image-quality. Just installing it will increase the quality to 85 but you can change it to whatever you want in the admin. Using 100 is actually not a good idea as it will greatly increase your file size and have no noticeable increase in quality from something more reasonable like 92. Also you may have to delete everything in pub/media/catalog/product/cache/. – David Stone Sep 6 at 19:38
  • @DavidStone thanks a lot! I'm currently working on other things but will implement this soon. The company that's going to use the template is one I work for, so I was compressing the images myself externally, to an appropriate size, then uploading them at 100% in magento. Is this still going to cause problems, or is it mainly in cases where a client has sent 100s of images and won't pay for the extra time it would take to manually fix them? – Joshua Flood Sep 17 at 10:34
1

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

enter image description here

  • As of Magento 2.2, modules are no longer stored within app/code/<Vendor_Name>/<Module_Name>, as explained in my original question. They are stored inside vendor/<Vendor_Name>/<Module_Name>. The app/code directory no longer exists in 2.2. See documentation here: devdocs.magento.com/guides/v2.2/architecture/archi_perspectives/… – Joshua Flood Dec 11 '17 at 14:39
  • I'm just not sure how I should override the Magento_Catalog app in order to alter the jpg quality in the Product model. – Joshua Flood Dec 11 '17 at 14:43
  • 1
    I don't know if you've already read this line - "But if you are creating a new module for distribution, you can just create the app/code/<vendor>/<type>-<module-name> directory and the required directories within it." There are many tutorials, you can check this one for overriding classes inchoo.net/magento-2/overriding-classes-magento-2 – Quan Le Dec 11 '17 at 16:24
  • And the files can still be read by magento, even though it does not own an app/code directory already created? Maybe I am just misunderstanding some of the terminology. – Joshua Flood Dec 11 '17 at 17:29
  • I think my problem is actuall in understanding the linked solution. They explain the di.xml file, as well as how to write the file which extends Magento/Catalog/Model/Product/Image, but they don't explain where to store that file exactly, or how it should be named. So when I enable the module I get an error which states <Vendor>/<Module>/Model/Product does not exist. – Joshua Flood Dec 12 '17 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.