{"record":{"id":"afb92eece9fe46dc","repo":"Intervention/image","slug":"gd-driver-can-only-decode-colors-in-integer-or-arr","errorCode":null,"errorMessage":"GD driver can only decode colors in integer or array format","messagePattern":"GD driver can only decode colors in integer or array format","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/ColorProcessor.php","lineNumber":75,"sourceCode":"        } catch (RuntimeException $e) {\n            throw new DriverException('Failed to export color', previous: $e);\n        }\n\n        return ($a << 24) + ($r << 16) + ($g << 8) + $b;\n    }\n\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;","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/ColorProcessor.php#L57-L93","documentation":"The GD color processor's import() only accepts two physical GD representations: an int (packed RGBA) or the array shape returned by imagecolorsforindex(). Any other type — string, null, object — is rejected up front. This is a type-contract error: the method is meant to unwrap GD natives, not to parse human color notations.","triggerScenarios":"Calling $driver->colorProcessor()->import('ff0000'), import(null), or import($colorObject); passing a hex string or an Intervention Color where a GD-native value was expected.","commonSituations":"Developers assuming import() is a generic color parser; wiring data from JSON configs (strings) straight into the driver API; adapter code ported from other libraries whose import() accepts strings.","solutions":["To build a Color from a string, use the Color class itself: Color::create('ff0000') — not the processor","Pass only values obtained from GD (imagecolorat() int or imagecolorsforindex() array) into import()","For driver-agnostic code, go through $manager->createDriver()->colorProcessor() only with native values you got from the same driver"],"exampleFix":"// before\n$color = $gdDriver->colorProcessor()->import('#ff0000');\n\n// after\nuse Intervention\\Image\\Colors\\Rgb\\Color;\n\n$color = Color::create('#ff0000'); // string parsing belongs to Color\n$gdColor = $gdDriver->colorProcessor()->import(imagecolorat($gd, 10, 10));","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"/** @param mixed $color */\nfunction isGdImportable(mixed $color): bool\n{\n    return is_int($color) || is_array($color);\n}","tryCatchPattern":"use Intervention\\Image\\Exceptions\\InvalidArgumentException;\n\ntry {\n    $color = $processor->import($value);\n} catch (InvalidArgumentException $e) {\n    $color = Intervention\\Image\\Colors\\Rgb\\Color::create((string) $value); // parse strings via Color\n}","preventionTips":["Route strings, hex, and named colors through Color::create() instead of import()","Reserve import() for values that originated from GD itself"],"tags":["gd","color","import","type-error","validation"],"backgroundTag":"invalid-color-format","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}