{"record":{"id":"0917199c29c0786c","repo":"Intervention/image","slug":"argument-extension-must-not-be-an-empty-string","errorCode":null,"errorMessage":"Argument $extension must not be an empty string","messagePattern":"Argument \\$extension must not be an empty string","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Encoders/FileExtensionEncoder.php","lineNumber":88,"sourceCode":"        }\n\n        return $image->encode(\n            $this->encoderByFileExtension(\n                $extension,\n            ),\n        );\n    }\n\n    /**\n     * Create matching encoder for given file extension\n     *\n     * @throws InvalidArgumentException\n     * @throws NotSupportedException\n     */\n    protected function encoderByFileExtension(string|FileExtension $extension): EncoderInterface\n    {\n        if ($extension === '') {\n            throw new InvalidArgumentException('Argument $extension must not be an empty string');\n        }\n\n        try {\n            $extension = is_string($extension) ? FileExtension::from(strtolower($extension)) : $extension;\n        } catch (Error) {\n            throw new NotSupportedException(\n                'Unable to find encoder for unknown image file extension \"' . $extension . '\"',\n            );\n        }\n\n        return $extension->format()->encoder(...$this->options);\n    }\n}\n","sourceCodeStart":70,"sourceCodeEnd":102,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Encoders/FileExtensionEncoder.php#L70-L102","documentation":"encoderByFileExtension() is the shared helper that maps an extension to a concrete encoder; it rejects an empty string immediately with InvalidArgumentException before attempting any enum lookup. In normal usage it is reached when the extension resolved at encode time - typically $image->origin()->fileExtension() - is an empty string rather than null, for example a source path with a trailing dot like 'photo.' whose pathinfo extension is ''.","triggerScenarios":"Encoding an image whose origin file extension resolved to '' (source filename ending in a dot, or sanitization code that stripped the extension down to an empty string), or a custom encoder subclass calling encoderByFileExtension('') directly.","commonSituations":"Oddly named source files such as 'banner.' or 'image..'; filename sanitizers that strip characters and leave no extension; subclassed encoders that pass computed extension values without checking them.","solutions":["Ensure the extension passed or resolved from the origin is a non-empty, supported value","Pass an explicit extension to the encoder instead of relying on origin metadata","If you subclass FileExtensionEncoder, guard the value before calling encoderByFileExtension()"],"exampleFix":"// before\n$ext = $image->origin()->fileExtension(); // '' for source 'photo.'\n$image->encode(new FileExtensionEncoder($ext));\n\n// after\n$ext = $image->origin()->fileExtension();\n$image->encode(new FileExtensionEncoder($ext ?: 'png'));","handlingStrategy":"validation","validationCode":"$ext = $image->origin()->fileExtension();\nif (!is_string($ext) || $ext === '') {\n    $ext = 'jpg';\n}\n$image->encode(new FileExtensionEncoder($ext));","typeGuard":null,"tryCatchPattern":"use Intervention\\Image\\Exceptions\\InvalidArgumentException;\n\ntry {\n    $image->encode(new FileExtensionEncoder($ext));\n} catch (InvalidArgumentException $e) {\n    $image->encode(new FileExtensionEncoder('png'));\n}","preventionTips":["Treat null and '' identically when reading origin()->fileExtension()","Reject or sanitize source filenames that end in a dot","In subclasses, never pass a computed extension to encoderByFileExtension() unchecked"],"tags":["php","intervention-image","encoder","file-extension","invalid-argument"],"backgroundTag":"empty-string-argument","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}