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

Failed to create CMYK color

Error message

Failed to create CMYK color

What it means

The Imagick ColorProcessor's export() builds an ImagickPixel for CMYK by setting the four channel values with setColorValue(); ImagickException/ImagickPixelException from those calls is wrapped as DriverException('Failed to create CMYK color'). The channels come from your Cmyk color normalized to 0..1, so failures trace to out-of-range or NaN values (channels constructed beyond 0-100) or an ImageMagick build with broken CMYK support.

Source

Thrown at src/Drivers/Imagick/ColorProcessor.php:67

        return $this->colorspace;
    }

    /**
     * @throws DriverException
     */
    public function export(ColorInterface $color): ImagickPixel
    {
        $color = $this->colorspace->importColor($color);

        if ($this->colorspace instanceof Cmyk) {
            try {
                $pixel = new ImagickPixel();
                $pixel->setColorValue(Imagick::COLOR_CYAN, $color->channel(Cyan::class)->normalized());
                $pixel->setColorValue(Imagick::COLOR_MAGENTA, $color->channel(Magenta::class)->normalized());
                $pixel->setColorValue(Imagick::COLOR_YELLOW, $color->channel(Yellow::class)->normalized());
                $pixel->setColorValue(Imagick::COLOR_BLACK, $color->channel(Key::class)->normalized());
            } catch (ImagickException | ImagickPixelException $e) {
                throw new DriverException('Failed to create CMYK color', previous: $e);
            }

            return $pixel;
        }

        $color = $color->toColorspace(Rgb::class);

        try {
            return new ImagickPixel(
                sprintf(
                    "srgba(%s, %s, %s, %s)",
                    $color->channel(Red::class)->value(),
                    $color->channel(Green::class)->value(),
                    $color->channel(Blue::class)->value(),
                    $color->channel(Alpha::class)->toString(),
                ),
            );
        } catch (ImagickException | ImagickPixelException $e) {

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Clamp C/M/Y/K channels to 0-100 before constructing the color so normalized() stays within 0..1.
  2. Prefer letting the library convert colors (e.g. $color->toColorspace(Cmyk\Colorspace::class)) instead of building raw CMYK values.
  3. If every CMYK operation fails, verify the ImageMagick build handles CMYK (convert logo: -colorspace CMYK info:) and rebuild/reinstall it.

Example fix

// before
$color = new \Intervention\Image\Colors\Cmyk\Color(120, -10, 50, 30); // channels outside 0-100

// after: clamp each channel into the valid 0-100 range
$c = fn (int $v): int => max(0, min(100, $v));
$color = new \Intervention\Image\Colors\Cmyk\Color($c(120), $c(-10), 50, 30);
Defensive patterns

Strategy: validation

Validate before calling

// keep CMYK channels inside 0-100 so normalized() stays in 0..1
$c = fn (int $v): int => max(0, min(100, $v));
$color = new \Intervention\Image\Colors\Cmyk\Color($c($cyan), $c($magenta), $c($yellow), $c($key));

Type guard

function clampChannel(int $value, int $min = 0, int $max = 100): int
{
    return max($min, min($max, $value));
}

Try / catch

try { $pixel = $processor->export($color); } catch (DriverException $e) { /* inspect chained ImagickPixelException; usually out-of-range channel values */ }

Prevention

When it happens

Trigger: Building Cmyk colors with channels outside 0-100 (unclamped math, decoded CMYK JPEGs with overshooting values) and using them for fill/text on a CMYK image; NaN channels produced by division-by-zero colorspace math.

Common situations: Print workflows constructing CMYK programmatically; hand-rolled RGB-to-CMYK formulas whose output exceeds gamut; older ImageMagick builds without full CMYK support.

Related errors


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