Skip to content

imagecolorsforindex(): Argument #2 ($color) is out of range #1299

@adrianbj

Description

@adrianbj

Short description of the issue

PHP 8 has changed the behavior of the shutup operator - it no longer silences fatal errors. This means that some old code in ImageSizerEngineGD.php (https://github.com/processwire/processwire/blob/d8945198f4a6a60dab23bd0462e8a6285369dcb9/wire/core/ImageSizerEngineGD.php#L865) now fails with the above error.

Expected behavior

We shouldn't be relying on the shutup operator in the first place :)

Actual behavior

image

You can test it with this image:
december-holidays-days-2-30-6753651837108830_3-law

Optional: Suggestion for a possible fix

Replace the above line (865) with this:

$transparentColor = $transparentIndex != -1 ? imagecolorsforindex($image, ($transparentIndex < imagecolorstotal($image) ? $transparentIndex : $transparentIndex - 1)) : 0;

which ensures the $transparentIndex is not greater or equal to the total number of colors in the image.

You can read more about it here: https://stackoverflow.com/questions/3874533/what-could-cause-a-color-index-out-of-range-error-for-imagecolorsforindex

Steps to reproduce the issue

  1. Use the above Google logo
  2. Attempt to resize it with one of the PW image sizing tools via GD

Setup/Environment

Server Details

Software Version
ProcessWire 3.0.170
PHP 8.0.0
Webserver Apache/2.4.46 (Unix)
MySQL Server 5.5.5-10.5.8-MariaDB
MySQL Client mysqlnd 8.0.0
Server Settings
Parameter Value
allow_url_fopen 1
max_execution_time 30 (changeable)
max_input_nesting_level 64
max_input_time 60
max_input_vars 1000
memory_limit 128M
post_max_size 50M
upload_max_filesize 2M
xdebug
xdebug.max_nesting_level
mod_rewrite 1
mod_security *confirmed off
EXIF Support 1
FreeType 1
GD Settings
Parameter Value
Version 2.3.0
GIF 1
JPG 1
PNG 1
WebP 1
Module Details
Module ClassName Version
AdminOnSteroids 2.0.21
BatchChildEditor 1.8.23
FieldtypeCombo 0.0.2
FieldtypeTable 0.2.0
FieldtypeTextareas 0.0.8
FileValidatorSvgSanitizer 0.0.3
FormBuilder 0.4.6
InputfieldCombo 0.0.2
InputfieldFormBuilderFile 0.0.2
InputfieldTable 0.2.0
InputfieldTextareas 0.0.7
ModuleReleaseNotes 0.11.1
ModuleSettingsImportExport 0.2.9
PageProtector 2.0.7
ProcessAdminActions 0.8.5
ProcessChildrenCsvExport 1.8.23
ProcessFormBuilder 0.4.6
ProcessPageListerPro 1.1.3
ProcessTableCsvExport 2.0.13
ProcessTracyAdminer 1.1.0
ProcessWireUpgrade 0.0.7
ProcessWireUpgradeCheck 0.0.7
TableCsvImportExport 2.0.13
TracyDebugger 4.21.35

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions