fixed gif image throwing error on frontend product page #37319
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Preconditions
Description (*)
Gif image loading issue on the product page,
imagecolorsforindex(): Argument #2 ($color) is out of range in /var/www/lib/Varien/Image/Adapter/Gd2.
Steps to Reproduce (*)
Set the image below as a product image and save it.

Reindex the indexer and clear the cache
Open that particular product in frontend.
Expected Result
Product page must load properly
Actual Result
Solution
When the product page loads some relevant gif image, it tries to resize it via Gd2 library of php with specific functionalities. It checks the transparency of the image and creates a new transparent background image according to the particular image if needed. In that, the color transparency index is returned by imagecolortransparent($imageResource) and it can be larger than the pallet size of the image in some cases.
The index of the transparency color is a property of the image. So it's possible that an image created with this index could be set outside the pallet size.
The Gd2.php file located is at :
vendor/magento/framework/Image/Adapter
imagecolortransparent($imageResource) should return -1 when the color transparency is greater than the pallet size of the image, which means that the image doesn't have any transparency
And when it calls applyTransparency() function for gif image at line 341 with $transparentIndex got by imagecolortransparent($imageResource), It checks the condition at line 346 where it checks
if ($transparentIndex >= 0 && $transparentIndex <= imagecolorstotal($this->_imageHandler)) .
But the transparent color index returned by imagecolortransparent() must be less than the pallet size of the image for an appropriate result and it shouldn't be equal to or greater than the pallet size.
Therefore we need to change condition as :
if ($transparentIndex >= 0 && $transparentIndex < imagecolorstotal($this->_imageHandler))
And if the condition satisfies then only it can look to get the new identifier for transparent color.
Questions or comments
Contribution checklist (*)