magento2: Magento 2.4.4 Mini cart item images not showing

Preconditions and environment

  • Magento 2.4.4
  • PHP8.1

Steps to reproduce

  1. Go to Content >> Design >> Configuration >>Edit theme >>Product Image Watermarks >> Thumbnail
  2. Upload image with Thumbnail and set image position as “Center”
  3. Add Products to Cart that all products have images.
  4. Open Minicart.
  5. Run bin/magento catalog:image:resize and observe the error in the terminal.

Expected result

All item images in Minicart get the images

Actual result

All item images are replaced by the default Product placeholder image

Additional information

I have debugged in this file vendor/magento/module-catalog/Helper/Image.php

public function getUrl()
    {
        try {
            switch ($this->mediaConfig->getMediaUrlFormat()) {
                case CatalogMediaConfig::IMAGE_OPTIMIZATION_PARAMETERS:
                    $this->initBaseFile();
                    break;
                case CatalogMediaConfig::HASH:
                    $this->applyScheduledActions();
                    break;
                default:
                    throw new LocalizedException(__("The specified Catalog media URL format is not supported."));
            }
            return $this->_getModel()->getUrl();
        } catch (\Exception $e) {
            \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info($e->getMessage());
            return $this->getDefaultPlaceholderUrl();
        }
    }

then got those errors

Deprecated Functionality: Implicit conversion from float 12.5 to int loses precision in /home/stagefish/public_html/stage.livefish.com.au/vendor/magento/framework/Image/Adapter/Gd2.php on line 963 [] []

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 24 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Hi,

I’ve recently come across this same issue. It is indeed caused by theme images with an odd dimension and the watermark position set to centre. If using the Gd2 adapter, it uses the following to calculate the position:

$positionX = $this->_imageSrcWidth / 2 - imagesx($watermark) / 2;
$positionY = $this->_imageSrcHeight / 2 - imagesy($watermark) / 2;

If the image size has an odd dimension, this results in a float. These are then passed to imagecopymergeWithAlphaFix on the next line, which passes them to imagecopy. This function expects integers and therefore gives the warning that the OP reported. I think the same is true for the ImageMagick adapter as it handles it in the same way.

For what it’s worth - when debugging this I didn’t see any warnings at all in the system or debug log, only managing to get the output the OP saw by wrapping the call to createWatermarkBasedOnPosition in a try/catch. The only sign I had that something was wrong was several missing folders in media/catalog/product/cache which relate to the sizes that were being silently skipped. Generating the images from the command line also gave no errors/warnings. It’s worth noting that I couldn’t find anything in the documentation regarding the use of odd dimensions with image sizes.

I patched this for our instances (2.4.4) by explicitly casting the resulting positions to integers. It works for us but your results may vary.

index af08cb5..2191f34 100644
--- a/vendor/magento/framework/Image/Adapter/Gd2.php
+++ b/vendor/magento/framework/Image/Adapter/Gd2.php
@@ -538,8 +538,8 @@ class Gd2 extends AbstractAdapter
         } elseif ($this->getWatermarkPosition() == self::POSITION_STRETCH) {
             $watermark = $this->createWaterMark($watermark, $this->_imageSrcWidth, $this->_imageSrcHeight);
         } elseif ($this->getWatermarkPosition() == self::POSITION_CENTER) {
-            $positionX = $this->_imageSrcWidth / 2 - imagesx($watermark) / 2;
-            $positionY = $this->_imageSrcHeight / 2 - imagesy($watermark) / 2;
+            $positionX = (int) ($this->_imageSrcWidth / 2 - imagesx($watermark) / 2);
+            $positionY = (int) ($this->_imageSrcHeight / 2 - imagesy($watermark) / 2);
             $this->imagecopymergeWithAlphaFix(
                 $this->_imageHandler,
                 $watermark,

And for ImageMagick:

index 5cfa7fe..099a960 100644
--- a/vendor/magento/framework/Image/Adapter/ImageMagick.php
+++ b/vendor/magento/framework/Image/Adapter/ImageMagick.php
@@ -326,8 +326,8 @@ class ImageMagick extends AbstractAdapter
                 $watermark->sampleImage($this->_imageSrcWidth, $this->_imageSrcHeight);
                 break;
             case self::POSITION_CENTER:
-                $positionX = ($this->_imageSrcWidth - $watermark->getImageWidth()) / 2;
-                $positionY = ($this->_imageSrcHeight - $watermark->getImageHeight()) / 2;
+                $positionX = (int) (($this->_imageSrcWidth - $watermark->getImageWidth()) / 2);
+                $positionY = (int) (($this->_imageSrcHeight - $watermark->getImageHeight()) / 2);
                 break;
             case self::POSITION_TOP_RIGHT:
                 $positionX = $this->_imageSrcWidth - $watermark->getImageWidth();

Awesome, thanks!