Magento2: Running 'bin/magento catalog:images:resize' doesn't check if an image was already resized before, therefore it is superslow as it always resizes all images

Created on 10 Feb 2020  路  11Comments  路  Source: magento/magento2

I'm pretty sure in Magento 2.1 (and maybe 2.2) when you ran bin/magento catalog:images:resize, it only resized images which weren't already resized before.

This no longer seems to be the case in Magento 2.3.
I think this behavior got introduced by https://github.com/magento/magento2/commit/498879059b400f8785ee63ba371798ffec33ae53 (not verified yet)

Preconditions (*)

  1. Magento 2.3.3
  2. PHP 7.2.26

Steps to reproduce (*)

  1. Have a shop with a product with an image
  2. Run bin/magento catalog:images:resize
  3. Look at the timestamp of the resized images in pub/media/catalog/product/cache/
  4. Wait a while
  5. Run bin/magento catalog:images:resize again
  6. Notice that the modified timestamp of images in pub/media/catalog/product/cache/ has been altered, which means the images got resized again even if they already exist.

Expected result (*)

  1. Executing bin/magento catalog:images:resize should not re-save an already resized image

Actual result (*)

  1. Executing bin/magento catalog:images:resize re-saves already resized images

Temp solution

This seems to resolve the issue after some very initial testing:

diff --git a/app/code/Magento/MediaStorage/Service/ImageResize.php b/app/code/Magento/MediaStorage/Service/ImageResize.php
index 6da48ee69ed..ccfce994752 100644
--- a/app/code/Magento/MediaStorage/Service/ImageResize.php
+++ b/app/code/Magento/MediaStorage/Service/ImageResize.php
@@ -287,13 +287,18 @@ class ImageResize
     private function resize(array $viewImage, string $originalImagePath, string $originalImageName)
     {
         $imageParams = $this->paramsBuilder->build($viewImage);
-        $image = $this->makeImage($originalImagePath, $imageParams);
         $imageAsset = $this->assertImageFactory->create(
             [
                 'miscParams' => $imageParams,
                 'filePath' => $originalImageName,
             ]
         );
+        if ($this->mediaDirectory->isFile($imageAsset->getPath())) {
+            // image was already resized, let's quit!
+            return;
+        }
+
+        $image = $this->makeImage($originalImagePath, $imageParams);

         if (isset($imageParams['watermark_file'])) {
             if ($imageParams['watermark_height'] !== null) {

Postponing the initialization of the $image variable also has a very big impact on performance.

Catalog MediaGallery MediaStorage Confirmed Format is valid Ready for Work Reported on 2.3.3 Reproduced on 2.4.x

Most helpful comment

Thanks @shikhamis11 for verifying!

I just tested some more on different clean Magento installations:

  • 2.1.18: images which were resized before aren't being saved again => good
  • 2.2.11: images which were resized before are being saved again => not good
  • 2.3.4: images which were resized before are being saved again => not good

All 11 comments

Hi @hostep. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • [x] Summary of the issue
  • [x] Information on your environment
  • [x] Steps to reproduce
  • [x] Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

@hostep do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [ ] yes
  • [x] no

Hi @shikhamis11. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 5. Add label Issue: Confirmed once verification is complete.

  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

:white_check_mark: Confirmed by @shikhamis11
Thank you for verifying the issue. Based on the provided information internal tickets MC-31322 were created

Issue Available: @shikhamis11, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._

Thanks @shikhamis11 for verifying!

I just tested some more on different clean Magento installations:

  • 2.1.18: images which were resized before aren't being saved again => good
  • 2.2.11: images which were resized before are being saved again => not good
  • 2.3.4: images which were resized before are being saved again => not good

Please make sure any mods to this area also work in database media storage mode. It is often overlooked.

@gwharton: I was aware of your recent changes in that area regarding the database storage, so I tried to take that into account in https://github.com/magento/magento2/pull/26801, but haven't actually tested it for real yet.

Ahh yes. I should have spotted that in the PR before commenting. Changes look good, although also untested here.

Hi @engcom-Alfa. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
  • [ ] 2. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.


The issue is still reproducible on 2.4-develop

@hostep Many thanks! Great job

https://github.com/magento/magento2/pull/26801 got merged a while ago, so we can close this issue.

No information was provided about in which Magento versions it will be fixed, but I'm guessing 2.4.0 and maybe 2.3.6?

Was this page helpful?
0 / 5 - 0 ratings