Intervention/image · error · NotSupportedException

Unable to find encoder for unknown image media type "' . $me

Error message

Unable to find encoder for unknown image media type "' . $mediaType . '"

What it means

MediaTypeEncoder::encoderByMediaType() maps the given media type to the MediaType enum via MediaType::from($mediaType) and converts the enum's ValueError into this NotSupportedException when no case matches. Matching is exact: unlike the file-extension path there is no strtolower() or parameter stripping, so 'Image/PNG' or 'image/jpeg; charset=utf-8' fail even though the base type is supported.

Source

Thrown at src/Encoders/MediaTypeEncoder.php:60

    {
        $mediaType = is_null($this->mediaType) ? $image->origin()->mediaType() : $this->mediaType;

        return $image->encode(
            $this->encoderByMediaType($mediaType),
        );
    }

    /**
     * Return new encoder by given media (MIME) type.
     *
     * @throws NotSupportedException
     */
    protected function encoderByMediaType(string|MediaType $mediaType): EncoderInterface
    {
        try {
            $mediaType = is_string($mediaType) ? MediaType::from($mediaType) : $mediaType;
        } catch (Error) {
            throw new NotSupportedException(
                'Unable to find encoder for unknown image media type "' . $mediaType . '"',
            );
        }

        return $mediaType->format()->encoder(...$this->options);
    }
}

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Normalize the input before passing: lowercase it and strip parameters: strtolower(explode(';', $mediaType)[0])
  2. Pass a MediaType enum case (e.g. MediaType::IMAGE_PNG) instead of a string
  3. Guard user- or header-supplied values with MediaType::tryFrom() and reject or default unsupported types

Example fix

// before
$image->encodeUsingMediaType($response->getHeaderLine('Content-Type')); // 'image/webp; charset=binary'

// after
$mediaType = strtolower(trim(explode(';', $response->getHeaderLine('Content-Type'))[0]));
$image->encodeUsingMediaType($mediaType); // 'image/webp'
Defensive patterns

Strategy: type-guard

Validate before calling

use Intervention\Image\MediaType;

$mediaType = strtolower(trim(explode(';', $headerValue)[0]));
if (MediaType::tryFrom($mediaType) === null) {
    throw new RuntimeException('Unsupported media type: ' . $mediaType);
}
$image->encodeUsingMediaType($mediaType);

Type guard

use Intervention\Image\MediaType;

function isSupportedMediaType(string $mediaType): bool
{
    $normalized = strtolower(trim(explode(';', $mediaType)[0]));

    return MediaType::tryFrom($normalized) !== null;
}

Try / catch

use Intervention\Image\Exceptions\NotSupportedException;

try {
    $image->encodeUsingMediaType($mediaType);
} catch (NotSupportedException $e) {
    $image->encodeUsingMediaType('image/png');
}

Prevention

When it happens

Trigger: Calling $image->encodeUsingMediaType('image/svg+xml'), passing 'image jpeg' (missing slash), an uppercase 'Image/PNG', a full Content-Type header with parameters such as 'image/png; charset=binary', or applying new MediaTypeEncoder($headerValue) with a value scraped from an HTTP Accept or Content-Type header.

Common situations: Forwarding Content-Type/Accept header values verbatim; storing MIME types with parameters in a database; case differences between producers; trying to encode unsupported formats like SVG.

Related errors


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