We recently upgraded to Magento 2. On our M1 install, we used a script which did direct database insertion for our product images. This had the effect of allowing us to use one image for many products. For example, a hat may have multiple sizes in the same color, but they can all share the same image. This made it very easy if we had to replace images, which was a simple file-level copy-and-replace script.

On upgrading to Magento 2, the script was rewritten to use Magento's internal functionality. However, now when importing images, each size gets its own copy, with a number appended to the end.

This means that now, we're using 4x or 5x more space for a given product, as well as adding the step of deleting all images (up to 6 per product) for all sizes of a given product, since we can't really use the replacement script. This could mean going in and having to edit 4 or 5 per product that we're updating. Needless to say, this is a less-than-ideal situation.

Is there any way to prevent Magento from appending that number? Preferably only when using the upload script?

1 Answer 1


Looked into this: Yes and no.

The magic happens in \Magento\Catalog\Model\Product\Gallery\Processor:

 * Add image to media gallery and return new filename
 * @param \Magento\Catalog\Model\Product $product
 * @param string $file file path of image in file system
 * @param string|string[] $mediaAttribute code of attribute with type 'media_image',
 *                                                      leave blank if image should be only in gallery
 * @param boolean $move if true, it will move source file
 * @param boolean $exclude mark image as disabled in product page view
 * @return string
 * @throws \Magento\Framework\Exception\LocalizedException
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
 * @SuppressWarnings(PHPMD.NPathComplexity)
public function addImage(
    \Magento\Catalog\Model\Product $product,
    $mediaAttribute = null,
    $move = false,
    $exclude = true
) {

Note the $move parameter. If that's false, it will leave the image wherever it is as given (within /pub/media/). If true, it will move it into /pub/media/catalog/product/#/#/file.jpg according to convention, avoiding duplicate filenames in the process.

What that means is, if $move comes through as false, it will leave it wherever you put it. You'll probably want to put it into the proper folder yourself though.

Where's that actually coming from, since it already defaults to false?


    $imageFileUri = $this->getMediaGalleryProcessor()->addImage(
        isset($newEntry['types']) ? $newEntry['types'] : [],
        isset($newEntry['disabled']) ? $newEntry['disabled'] : true

...hardcoded to true. Awesome.

So to make it happen through the service layer (adding the image via the gallery manager, which in turn saves the product via the repository), you would have to intercept and change that flag before it hits addImage(). It's a public method, so a before plugin should be able to do that.

If you want it to only change the flag for a custom import, you would have to maintain some custom state or check the app area to do so.

  • Where do I look for to str_replace Underscore(_) to Dashes (-) in b4 the version? Commented Jul 15, 2021 at 18:52
  • Mag2.3.5 found this in method public static function getNewFileName($destinationFile) in /htdocs/vendor/magento/framework/File/Uploader.php around line 689 $baseName = $fileInfo['filename'] . '_' . $index . '.' . $fileInfo['extension']; but this method is static not sure I can use plugin to extend this? Commented Jul 15, 2021 at 19:09

Your Answer

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

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