1

I created a module that overrides ImageUploader like so:

<preference 
    for="Magento\Catalog\Model\ImageUploader" 
    type="MyCompany\MyModule\Model\Catalog\ImageUploader" />

The code looks like this (don't ask why, but we are only allowing jpg images):

<?php

namespace MyCompany\MyModule\Model\Catalog;

class ImageUploader extends \Magento\Catalog\Model\ImageUploader {

    /**
     * @param string[] $allowedExtensions
     */
    public function setAllowedExtensions($allowedExtensions)
    {
        parent::setAllowedExtensions(['jpg', 'jpeg']);
    }
}

I installed the module and the other parts work just fine. But this does not prevent me from uploading a PNG. Anyone have any ideas of what I might be doing wrong or what I should override instead of this?

Thanks!

0

Take a look at vendor/magento/module-catalog/etc/di.xml

    <virtualType name="Magento\Catalog\CategoryImageUpload" type="Magento\Catalog\Model\ImageUploader">
        <arguments>
            <argument name="baseTmpPath" xsi:type="string">catalog/tmp/category</argument>
            <argument name="basePath" xsi:type="string">catalog/category</argument>
            <argument name="allowedExtensions" xsi:type="array">
                <item name="jpg" xsi:type="string">jpg</item>
                <item name="jpeg" xsi:type="string">jpeg</item>
                <item name="gif" xsi:type="string">gif</item>
                <item name="png" xsi:type="string">png</item>
            </argument>
        </arguments>
    </virtualType>

In this case, we need to override Magento\Catalog\CategoryImageUpload, not Magento\Catalog\Model\ImageUploader.

Our custom class will be the virtual type class. So, we need to re-inject the dependencies.

Your di.xml should be:

<?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\CategoryImageUpload"
            type="Vendor\Module\Model\Catalog\ImageUploader" />

    <virtualType name="Vendor\Module\Model\Catalog\ImageUploader" type="Magento\Catalog\Model\ImageUploader">
        <arguments>
            <argument name="baseTmpPath" xsi:type="string">catalog/tmp/category</argument>
            <argument name="basePath" xsi:type="string">catalog/category</argument>
            <argument name="allowedExtensions" xsi:type="array">
                <item name="jpg" xsi:type="string">jpg</item>
                <item name="jpeg" xsi:type="string">jpeg</item>
                <item name="gif" xsi:type="string">gif</item>
                <!--<item name="png" xsi:type="string">png</item>-->
            </argument>
        </arguments>
    </virtualType>
</config>
  • I'm on 2.1.6 and the \Magento\Catalog\CategoryImageUpload class does not exist. I did try your suggestion using \Magento\Catalog\Model\ImageUploader class, but that didn't work. I am now attempting to override \Magento\MediaStorage\Model\File\Uploader and seem to be making more progress there. Thanks for the tips though. It got me looking into some different places. – Tony DeStefano Jan 4 at 16:44
  • Yep, overriding \Magento\MediaStorage\Model\File\Uploader worked perfectly. – Tony DeStefano Jan 4 at 16:51

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.