Skip to content

fixed gif image throwing error on frontend product page #37319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

fixed gif image throwing error on frontend product page #37319

wants to merge 2 commits into from

Conversation

bhoopatparmar
Copy link

Preconditions

  1. PHP 8.1
  2. Magento 2.4.5-p1

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 (*)

  1. Set the image below as a product image and save it.
    image

  2. Reindex the indexer and clear the cache

  3. Open that particular product in frontend.

Expected Result

Product page must load properly

Actual Result

ValueError: imagecolorsforindex(): Argument #2 ($color) is out of range in /var/www/public_html/vendor/magento/framework/Image/Adapter/Gd2.php:347
Stack trace:
#0 /var/www/public_html/vendor/magento/framework/Image/Adapter/Gd2.php(347): imagecolorsforindex(Object(GdImage), 255)
#1 /var/www/public_html/vendor/magento/framework/Image/Adapter/Gd2.php(288): Magento\Framework\Image\Adapter\Gd2->applyTransparency(Object(GdImage), 255)
#2 /var/www/public_html/vendor/magento/framework/Image/Adapter/Gd2.php(432): Magento\Framework\Image\Adapter\Gd2->fillBackgroundColor(Object(GdImage))
#3 /var/www/public_html/generated/code/Magento/Framework/Image/Adapter/Gd2/Interceptor.php(59): Magento\Framework\Image\Adapter\Gd2->resize(240, 300)
#4 /var/www/public_html/vendor/magento/framework/Image.php(115): Magento\Framework\Image\Adapter\Gd2\Interceptor->resize(240, 300)

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 (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented Mar 31, 2023

Hi @bhoopatparmar. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.

Add the comment under your pull request to deploy test or vanilla Magento instance:
  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@bhoopatparmar
Copy link
Author

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@engcom-Bravo engcom-Bravo added the Priority: P3 May be fixed according to the position in the backlog. label Mar 31, 2023
@bhoopatparmar
Copy link
Author

The changes requested in this PR are already merged in 2.4-develop branch by internal tickets ACP2E-1632. SO, I'm closing this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: P3 May be fixed according to the position in the backlog.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants