After recently upgrading one of our existing Magento 2 clients from Magento 2.1.4 up to Magento 2.1.6 an issue was reported whereby images on the front-end were not displaying correctly.
Upon loading a configurable product, product images were displayed correctly; however, when making a product selection i.e. (Red, Blue, Green) Magento will update the product page to show the Images from the simple product as the main image. In this scenario, the product images were significantly smaller than they had appeared previously.
The following article discusses our steps to resolving the issue in the hope that we can help other merchants and developers that are having similar issues after upgrading to 2.1.6.
The 2.1.6 update to Magento introduced a change to how the platform handles images associated to custom themes; specifically, they introduced a 'frame' element to add white space around product images.
The setting for the 'frame' element is set to true by default, so if your store is using a custom theme (which will be most stores) and doesn't include the frame element then Magento will use the value assigned to the 'product_image_white_borders' option which usually has a value of 1 but is set to 0 for the default Luma and Blank themes.
This subsequently means that when running 'bin/magento catalog:images:resize' command you will start to see issues.
Fixing the issue
Update: Magento has acknowledged the issue and have has released an official patch which is linked in the resources section at the end of this article.
At the time of starting our investigation, this patch was not available to us and we've taken a slightly different route to resolving the issue which we still think holds value.
Step 1 - Review your theme
The first step is to review the active themes your store/stores are using and to add the frame nodes. We've included an extract below.
Step 2 - Resizing images
The next step would be to deploy the website and run the image re-size command listed below.
Working with image cache
However, this is where we encountered another issue. When running this command, Magento will not create another image size if one already exists in the image cache (stored in the media/catalog/product folder). This was a problem for us as the new smaller images were already being returned from the cache and we didn't want them to be skipped over.
What's more, we discovered that if you delete this cache manually through ssh or via the Magento admin using "System->Cache Managament->Flush Catalog Images Cache" then Magento will not be able to serve images from the Cache and will not generate a new cached image when the product page is loaded again.
This subsequently means that users will see missing images until the resize command has completed (which can take an extremely long time and is dependant on how many products you have).
A solution to the problem
Our solution was to override the Magento Catalog module's Image Helper so that when the resize functionality is processed we add an extra condition to check to see if the call has been made from the CLI which allows us to overwrite images that are already in the cache (You can view the Gist here)
At CTI we like to apply our patches in a non-obtrusive manner, so, in order to apply the patch to Magento we use Composer Patcher which gives us the ability to apply GIT Patches using a post install hook.
Using this method allows us to easily remove the patch again in the future.
Bonus Tip: Did you know when requesting patches from Magento for a Magento Enterprise Customer you can ask for a Composer Patch so you can add them more easily.
Closing thoughts & thanks
As Magento works hard to improve Magento 2 we expect to see some changes to the image resizer command. Notably, we'd like to see the following changes.
1. The Image Resize Command takes a long time to progress with no easy way of viewing progress. It would be great to see them use the Symfony Console Progress Component here.
2. The Image Resize Command processes images across all themes (see Github Issue 8145) It would be great to be able to specify themes to include and exclude using option arguments similar to how we do this with the static content deploy command.
Finally a big thanks to one of our new recruits Andy Irvine (@andy_irvine) for working across this issue.
We hope you found this article useful.
- Magento 2.1.6 - Release Notes
- Magento Dev Docs Article - Image Resize Issue with Magento version 2.1.6
- Composer Patches
- GitHub Gist