1

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

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,
    $file,
    $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?

\Magento\Catalog\Model\ProductRepository->processNewMediaGalleryEntry():

    $imageFileUri = $this->getMediaGalleryProcessor()->addImage(
        $product,
        $tmpFilePath,
        isset($newEntry['types']) ? $newEntry['types'] : [],
        true,
        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.

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.