{"record":{"id":"f0f1768e3d1f5abb","repo":"Intervention/image","slug":"gd-driver-can-only-decode-array-color-format-array","errorCode":null,"errorMessage":"GD driver can only decode array color format array{red: int, green: int, blue: int, alpha: int}","messagePattern":"GD driver can only decode array color format array(.+?)","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/ColorProcessor.php","lineNumber":81,"sourceCode":"\n    /**\n     * {@inheritdoc}\n     *\n     * @see ColorProcessorInterface::import()\n     *\n     * @throws InvalidArgumentException\n     * @throws DriverException\n     */\n    public function import(mixed $color): ColorInterface\n    {\n        if (!is_int($color) && !is_array($color)) {\n            throw new InvalidArgumentException('GD driver can only decode colors in integer or array format');\n        }\n\n        if (is_array($color)) {\n            // array conversion\n            if (!$this->isValidArrayColor($color)) {\n                throw new InvalidArgumentException(\n                    'GD driver can only decode array color format array{red: int, green: int, blue: int, alpha: int}',\n                );\n            }\n\n            $r = $color['red'];\n            $g = $color['green'];\n            $b = $color['blue'];\n            $a = $color['alpha'];\n        } else {\n            // integer conversion\n            $a = ($color >> 24) & 0xFF;\n            $r = ($color >> 16) & 0xFF;\n            $g = ($color >> 8) & 0xFF;\n            $b = $color & 0xFF;\n        }\n\n        try {\n            // convert gd apha integer to intervention alpha integer","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/ColorProcessor.php#L63-L99","documentation":"import() accepted that the value is an array, but isValidArrayColor() (src/Drivers/Gd/ColorProcessor.php:120) requires all four integer keys red, green, blue, alpha with int values — exactly the shape imagecolorsforindex() returns. Missing keys, differently-named keys (r/g/b/a), or string values fail this check.","triggerScenarios":"Passing ['r' => 255, 'g' => 0, 'b' => 0, 'a' => 0]; passing ['red' => '255', ...] with string values from a database/JSON; arrays missing the alpha entry (e.g. hand-built 3-element RGB).","commonSituations":"Persisted color arrays from other libraries or user settings stored as JSON; arrays built by array_map('strval', ...) pipelines; partial arrays copied from documentation snippets.","solutions":["Normalize keys and cast to int before importing: array_map(fn($v) => (int) $v, ['red' => $c['r'], 'green' => $c['g'], 'blue' => $c['b'], 'alpha' => $c['a'] ?? 0])","Feed import() exclusively with the untouched output of imagecolorsforindex($gd, $index)","For hex/array user input, prefer Color::create() which accepts more shapes"],"exampleFix":"// before\n$color = $processor->import(['r' => 255, 'g' => 0, 'b' => 0]);\n\n// after\n$color = $processor->import([\n    'red' => 255, 'green' => 0, 'blue' => 0, 'alpha' => 0,\n]);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"/** @param array<mixed> $color */\nfunction isGdColorArray(array $color): bool\n{\n    foreach (['red', 'green', 'blue', 'alpha'] as $key) {\n        if (!array_key_exists($key, $color) || !is_int($color[$key])) {\n            return false;\n        }\n    }\n\n    return true;\n}","tryCatchPattern":"use Intervention\\Image\\Exceptions\\InvalidArgumentException;\n\ntry {\n    $color = $processor->import($array);\n} catch (InvalidArgumentException $e) {\n    $color = new Intervention\\Image\\Colors\\Rgb\\Color(\n        (int) ($array['red'] ?? $array['r'] ?? 0),\n        (int) ($array['green'] ?? $array['g'] ?? 0),\n        (int) ($array['blue'] ?? $array['b'] ?? 0),\n        0,\n    );\n}","preventionTips":["Pass imagecolorsforindex() output verbatim; never rebuild the array by hand with renamed keys","Cast JSON/database values to int and normalize r/g/b/a keys before import()"],"tags":["gd","color","import","array-shape","validation"],"backgroundTag":"invalid-color-format","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}