Intervention/image · error · ModifierException

Failed to convert colorspace to Imagick constant

Error message

Failed to convert colorspace to Imagick constant

What it means

Thrown inside ColorspaceModifier::imagickColorspaceOrFail() when resolving the Oklab/Oklch Imagick constants raises a PHP Error (e.g. from constant() or a class-resolution failure inside the try). It is a narrow diagnostic wrapper: the defined() checks normally prevent undefined-constant access, so reaching the catch means an unexpected engine-level Error occurred while fetching the constant.

Source

Thrown at src/Drivers/Imagick/Modifiers/ColorspaceModifier.php:85

        if ($colorspace instanceof Hsl) {
            return Imagick::COLORSPACE_HSL;
        }

        if ($colorspace instanceof Hsv) {
            return Imagick::COLORSPACE_HSB;
        }

        try {
            if ($colorspace instanceof Oklab && defined(Imagick::class . '::COLORSPACE_OKLAB')) {
                return constant(Imagick::class . '::COLORSPACE_OKLAB');
            }

            if ($colorspace instanceof Oklch && defined(Imagick::class . '::COLORSPACE_OKLCH')) {
                return constant(Imagick::class . '::COLORSPACE_OKLCH');
            }
        } catch (Error $e) {
            throw new ModifierException(
                'Failed to convert colorspace to Imagick constant',
                previous: $e,
            );
        }

        throw new NotSupportedException('Colorspace ' . $colorspace::class . ' is not supported by driver');
    }
}

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Inspect $e->getPrevious() — it holds the actual PHP Error explaining the retrieval failure.
  2. Update ext-imagick and ImageMagick to a coherent recent pair where Oklab/Oklch constants are fully exposed (ImageMagick 7.1+/recent pecl/imagick).
  3. If the environment cannot reliably expose the constant, target a well-supported colorspace (sRGB) instead.
  4. Verify manually: php -r 'var_dump(defined("\\Imagick::COLORSPACE_OKLAB"));' before relying on Oklab in that environment.

Example fix

// before
$image->colorspace(\Intervention\Image\Colors\Oklab\Colorspace::class); // Error caught -> ModifierException

// after
if (defined(\Imagick::class . '::COLORSPACE_OKLAB')) {
    $image->colorspace(\Intervention\Image\Colors\Oklab\Colorspace::class);
} else {
    $image->colorspace(\Intervention\Image\Colors\Rgb\Colorspace::class);
}
Defensive patterns

Strategy: validation

Validate before calling

if (!defined(\Imagick::class . '::COLORSPACE_OKLAB')) {
    // do not attempt Oklab on this build
    $target = \Intervention\Image\Colors\Rgb\Colorspace::class;
}

Type guard

function imagickSupports(string $constant): bool
{
    return defined(\Imagick::class . '::' . $constant);
}

Try / catch

use Intervention\Image\Exceptions\ModifierException;
try {
    $image->colorspace(\Intervention\Image\Colors\Oklab\Colorspace::class);
} catch (ModifierException $e) {
    // Error-derived previous: usually a broken extension — fall back to sRGB
    $image->colorspace(\Intervention\Image\Colors\Rgb\Colorspace::class);
}

Prevention

When it happens

Trigger: Selecting Oklab or Oklch as target colorspace on a build where COLORSPACE_OKLAB/COLORSPACE_OKLCH is reported as defined but constant() retrieval fails (broken extension state), or where an autoload/Error condition fires inside the try block. Rare in practice; most unsupported builds fall through to NotSupportedException instead.

Common situations: Bleeding-edge ImageMagick/imagick combinations with partially exposed Oklab constants; exotic runtimes where class constants behave inconsistently; usually encountered while diagnosing Oklab/Oklch support differences across environments.

Related errors


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