{"record":{"id":"6dfae729ccb6192c","repo":"Intervention/image","slug":"unable-to-find-file-extension-from-empty-string","errorCode":null,"errorMessage":"Unable to find file extension from empty string","messagePattern":"Unable to find file extension from empty string","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Encoders/FileExtensionEncoder.php","lineNumber":34,"sourceCode":"{\n    /**\n     * Encoder options.\n     *\n     * @var array<int|string, mixed>\n     */\n    protected array $options = [];\n\n    /**\n     * Create new encoder instance to encode to format of given file extension.\n     *\n     * @param null|string|FileExtension $extension Target file extension for example \"png\"\n     * @throws InvalidArgumentException\n     * @throws NotSupportedException\n     */\n    public function __construct(public null|string|FileExtension $extension = null, mixed ...$options)\n    {\n        if ($extension === '') {\n            throw new InvalidArgumentException('Unable to find file extension from empty string');\n        }\n\n        $mediaType = null;\n\n        if (is_string($extension)) {\n            try {\n                $mediaType = FileExtension::from(strtolower($extension))->mediaType();\n            } catch (Error) {\n                throw new NotSupportedException(\n                    'Unable to find encoder for unknown file extension \"' . $extension . '\"',\n                );\n            }\n        }\n\n        if ($extension instanceof FileExtension) {\n            $mediaType = $extension->mediaType();\n        }\n","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Encoders/FileExtensionEncoder.php#L16-L52","documentation":"InvalidArgumentException from the FileExtensionEncoder constructor: an empty string '' is explicitly rejected because it cannot be mapped to any media type. Note the distinction - null is valid (the encoder then falls back to the image's origin file extension at encode time) and unknown non-empty strings throw NotSupportedException instead; only exactly '' hits this check.","triggerScenarios":"new FileExtensionEncoder($ext) where $ext === '' - typically $ext = pathinfo($filename, PATHINFO_EXTENSION) on a filename without an extension, or an empty form field cast to string.","commonSituations":"Deriving the target format from user-supplied filenames that lack extensions; 'encode to same format as input' logic where the input name has no suffix; whitespace-only input trimmed to '' upstream.","solutions":["Pass null instead of '' when the origin extension should be used: $ext = $ext ?: null","Validate $extension !== '' before constructing the encoder","Use the FileExtension enum (e.g. FileExtension::PNG) instead of raw strings when the target is known","Reject or normalize extension-less filenames at the upload boundary"],"exampleFix":"// before\n$ext = pathinfo($filename, PATHINFO_EXTENSION); // '' for 'photo'\n$encoded = $image->encode(new FileExtensionEncoder($ext));\n\n// after\n$ext = pathinfo($filename, PATHINFO_EXTENSION) ?: null;\n$encoded = $image->encode(new FileExtensionEncoder($ext));","handlingStrategy":"validation","validationCode":"$extension = ($extension === '') ? null : $extension;\n$encoded = $image->encode(new FileExtensionEncoder($extension));","typeGuard":"function isValidFileExtension(null|string|FileExtension $extension): bool\n{\n    return $extension === null || $extension instanceof FileExtension || $extension !== '';\n}","tryCatchPattern":"use Intervention\\Image\\Exceptions\\InvalidArgumentException as ImageInvalidArgumentException;\n\ntry {\n    $encoder = new FileExtensionEncoder($extension);\n} catch (ImageInvalidArgumentException $e) {\n    $encoder = new FileExtensionEncoder(null); // fall back to origin extension\n}","preventionTips":["Convert empty strings to null before building encoders: $ext = $ext ?: null","Use the FileExtension enum when the target format is known at code time","Reject extension-less filenames at the upload boundary"],"tags":["encoder","file-extension","argument-validation"],"backgroundTag":"empty-argument","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}