{"record":{"id":"544cf5e346b2ee37","repo":"Intervention/image","slug":"unable-to-find-encoder-for-unknown-image-media-typ","errorCode":null,"errorMessage":"Unable to find encoder for unknown image media type \"' . $mediaType . '\"","messagePattern":"Unable to find encoder for unknown image media type \"' \\. \\$mediaType \\. '\"","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Encoders/MediaTypeEncoder.php","lineNumber":60,"sourceCode":"    {\n        $mediaType = is_null($this->mediaType) ? $image->origin()->mediaType() : $this->mediaType;\n\n        return $image->encode(\n            $this->encoderByMediaType($mediaType),\n        );\n    }\n\n    /**\n     * Return new encoder by given media (MIME) type.\n     *\n     * @throws NotSupportedException\n     */\n    protected function encoderByMediaType(string|MediaType $mediaType): EncoderInterface\n    {\n        try {\n            $mediaType = is_string($mediaType) ? MediaType::from($mediaType) : $mediaType;\n        } catch (Error) {\n            throw new NotSupportedException(\n                'Unable to find encoder for unknown image media type \"' . $mediaType . '\"',\n            );\n        }\n\n        return $mediaType->format()->encoder(...$this->options);\n    }\n}\n","sourceCodeStart":42,"sourceCodeEnd":68,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Encoders/MediaTypeEncoder.php#L42-L68","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Normalize the input before passing: lowercase it and strip parameters: strtolower(explode(';', $mediaType)[0])","Pass a MediaType enum case (e.g. MediaType::IMAGE_PNG) instead of a string","Guard user- or header-supplied values with MediaType::tryFrom() and reject or default unsupported types"],"exampleFix":"// before\n$image->encodeUsingMediaType($response->getHeaderLine('Content-Type')); // 'image/webp; charset=binary'\n\n// after\n$mediaType = strtolower(trim(explode(';', $response->getHeaderLine('Content-Type'))[0]));\n$image->encodeUsingMediaType($mediaType); // 'image/webp'","handlingStrategy":"type-guard","validationCode":"use Intervention\\Image\\MediaType;\n\n$mediaType = strtolower(trim(explode(';', $headerValue)[0]));\nif (MediaType::tryFrom($mediaType) === null) {\n    throw new RuntimeException('Unsupported media type: ' . $mediaType);\n}\n$image->encodeUsingMediaType($mediaType);","typeGuard":"use Intervention\\Image\\MediaType;\n\nfunction isSupportedMediaType(string $mediaType): bool\n{\n    $normalized = strtolower(trim(explode(';', $mediaType)[0]));\n\n    return MediaType::tryFrom($normalized) !== null;\n}","tryCatchPattern":"use Intervention\\Image\\Exceptions\\NotSupportedException;\n\ntry {\n    $image->encodeUsingMediaType($mediaType);\n} catch (NotSupportedException $e) {\n    $image->encodeUsingMediaType('image/png');\n}","preventionTips":["Normalize header values: lowercase and strip '; parameter' suffixes","Pass MediaType enum cases instead of strings where possible","Treat media types from external systems as untrusted input"],"tags":["php","intervention-image","encoder","media-type","http-headers","not-supported"],"backgroundTag":"unsupported-media-type","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}