Intervention/image · error · Intervention\Image\Exceptions\InvalidArgumentException

Invalid cmyk() color syntax "{input}"

Error message

Invalid cmyk() color syntax "{input}"

What it means

The CMYK string decoder only accepts strings matching 'cmyk(...)' with exactly four non-negative numeric components, each optionally suffixed with '%', separated by a comma (with optional space) or a single space: cmyk(100, 0, 0, 0) or cmyk(100%, 0%, 0%, 0%). Anything else that still begins with 'cmyk' - wrong component count, negative numbers, an alpha part, missing parenthesis - triggers this InvalidArgumentException.

Source

Thrown at src/Colors/Cmyk/Decoders/StringColorDecoder.php:48

            return false;
        }

        if (!str_starts_with(strtolower($input), 'cmyk')) {
            return false;
        }

        return true;
    }

    /**
     * Decode CMYK color strings
     *
     * @throws InvalidArgumentException
     */
    public function decode(mixed $input): ColorInterface
    {
        if (preg_match(self::PATTERN, (string) $input, $matches) !== 1) {
            throw new InvalidArgumentException('Invalid cmyk() color syntax "' . $input . '"');
        }

        $values = array_map(function (string $value): int {
            return intval(round(floatval(trim(str_replace('%', '', $value)))));
        }, [$matches['c'], $matches['m'], $matches['y'], $matches['k']]);

        return new Color(...$values);
    }
}

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Correct the string to four non-negative components: integers 0-100 or percentages, with no alpha part
  2. Remove the alpha component - the decoder cannot parse it; apply alpha separately via Cmyk\Color::create($c, $m, $y, $k, $alpha)
  3. Normalize user input before parsing (trim, strip whitespace variants, replace Unicode punctuation)
  4. If the value is dynamic, build programmatically: Cmyk\Color::create(100, 0, 0, 0) instead of string parsing

Example fix

// before
$color = Cmyk\Color::parse('cmyk(100%, 0%, 0%, 0% / 0.5)');

// after
$color = Cmyk\Color::parse('cmyk(100%, 0%, 0%, 0%)');
$color = Cmyk\Color::create(100, 0, 0, 0, 0.5); // needs alpha?
Defensive patterns

Strategy: validation

Validate before calling

if (!preg_match('/^cmyk ?\(([0-9.]+%?)(, ?| )([0-9.]+%?)(, ?| )([0-9.]+%?)(, ?| )([0-9.]+%?)\)$/i', trim($input))) {
    // reject before calling Cmyk\Color::parse()
    throw new InvalidArgumentException('Bad cmyk string: ' . $input);
}

Type guard

function isCmykString(mixed $input): bool
{
    return is_string($input)
        && str_starts_with(strtolower(trim($input)), 'cmyk(')
        && substr_count($input, ',') + substr_count(trim($input), ' ') >= 3;
}

Try / catch

use Intervention\Image\Exceptions\InvalidArgumentException as ImageArg;

try {
    $color = Cmyk\Color::parse($input);
} catch (ImageArg $e) {
    // invalid syntax; use a default or re-ask the user
    $color = Cmyk\Color::create(0, 0, 0, 100);
}

Prevention

When it happens

Trigger: Cmyk\Color::parse('cmyk(100, 0, 0)') (only 3 values); 'cmyk(100%, 0%, 0%, 0%, 0.5)' (alpha is not supported); 'cmyk(-10, 0, 0, 0)' (negative); 'cmyk(100,0,0,0' (missing ')'); 'cmyk(101%%, 0, 0, 0)' (malformed percent); smart quotes or non-ASCII dashes copied from design tools.

Common situations: Color strings copied from CSS/design software that append alpha ('/ 0.5' syntax), typo'd component counts in config files or CMS fields, user-supplied color input from forms passed straight to the decoder, or text editors auto-replacing hyphens/quotes with Unicode variants.

Related errors


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