Skip to content

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 #26796

Closed
@hostep

Description

@hostep

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 4988790 (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.

Metadata

Metadata

Labels

Component: CatalogComponent: MediaGalleryComponent: MediaStorageIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReported on 2.3.3Indicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branch

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions