How did I fix «imagecolorsforindex(): Argument #2 ($color) is out of range» for GIF images in Magento < 2.4.7?

Step 1

Replace the code:

with

Step 2

Replace the code:

with