Intervention/image · error · InvalidArgumentException

Failed to create color from input

Error message

Failed to create color from input "{input}"

What it means

NamedColor::create() accepts only exact CSS color names backed by the NamedColor enum (about 140 values like 'red', 'aliceblue', 'navy'). The input is lowercased and passed to the enum's from(); an unknown value makes from() throw ValueError, which this method converts into an InvalidArgumentException echoing your input.

Solutions

  1. Use an exact CSS named color (check the NamedColor enum cases for the accepted list)
  2. For hex/rgb()/rgba() strings, do not use NamedColor::create; pass them to the driver's color decoding or build an Rgb\Color directly
  3. Validate the name first with NamedColor::tryFrom(strtolower($name)) and fall back to a default color

Example fix

// before
$color = NamedColor::create($userColor);

// after
$color = NamedColor::tryFrom(strtolower($userColor))
    ? NamedColor::create($userColor)
    : NamedColor::create('white');
Defensive patterns

Strategy: type-guard

Validate before calling

$name = strtolower(trim($name));
if (NamedColor::tryFrom($name) === null) {
    $name = 'white';
}

Type guard

function isNamedColor(string $value): bool
{
    return NamedColor::tryFrom(strtolower($value)) !== null;
}

Try / catch

try {
    $color = NamedColor::create($input);
} catch (\Intervention\Image\Exceptions\InvalidArgumentException $e) {
    $color = NamedColor::create('white');
}

Prevention

When it happens

Trigger: NamedColor::create('redd') (typo), NamedColor::create('grey') (British spelling, not a case), NamedColor::create('#ff0000') (hex is not a named color), or any string not present in the NamedColor enum cases.

Common situations: User- or CMS-supplied color names; expecting the full CSS color parser (hex, rgb(), hsl()) from a named-color-only API; localized color names.

Related errors


AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23). Data as JSON: /api/errors/26e9fd4484340808. Report an issue: GitHub.

Appendix: source

Thrown at src/Colors/Rgb/NamedColor.php:171

    case TURQUOISE = 'turquoise';
    case VIOLET = 'violet';
    case WHEAT = 'wheat';
    case WHITE = 'white';
    case WHITESMOKE = 'whitesmoke';
    case YELLOW = 'yellow';
    case YELLOWGREEN = 'yellowgreen';

    /**
     * Create new named color.
     *
     * @throws InvalidArgumentException
     */
    public static function create(string $input): ColorInterface
    {
        try {
            return self::from(strtolower($input));
        } catch (TypeError | ValueError) {
            throw new InvalidArgumentException('Failed to create color from input "' . $input . '"');
        }
    }

    /**
     * Create new named color or return null of creation fails.
     */
    public static function tryCreate(string $input): ?ColorInterface
    {
        try {
            return self::from(strtolower($input));
        } catch (Error) {
            return null;
        }
    }

    /**
     * {@inheritdoc}
     *

View on GitHub (pinned to 5598b9e397)