{"record":{"id":"1a6f237a105ca5a9","repo":"Intervention/image","slug":"unable-to-decode-binary-data-from-empty-string","errorCode":null,"errorMessage":"Unable to decode binary data from empty string","messagePattern":"Unable to decode binary data from empty string","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/Decoders/BinaryImageDecoder.php","lineNumber":55,"sourceCode":"     *\n     * @throws InvalidArgumentException\n     * @throws ImageDecoderException\n     * @throws DriverException\n     * @throws StateException\n     * @throws NotSupportedException\n     */\n    public function decode(mixed $input): ImageInterface\n    {\n        if (!is_string($input) && !$input instanceof Stringable) {\n            throw new InvalidArgumentException(\n                'Image source must be binary data of type string or instance of ' . Stringable::class,\n            );\n        }\n\n        $input = (string) $input;\n\n        if ($input === '') {\n            throw new InvalidArgumentException('Unable to decode binary data from empty string');\n        }\n\n        return $this->isGifFormat($input) ? $this->decodeGif($input) : $this->decodeBinary($input);\n    }\n\n    /**\n     * Decode image from given binary data\n     *\n     * @throws InvalidArgumentException\n     * @throws ImageDecoderException\n     * @throws DriverException\n     * @throws StateException\n     * @throws NotSupportedException\n     */\n    private function decodeBinary(string $input): ImageInterface\n    {\n        $gd = @imagecreatefromstring($input);\n","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/Decoders/BinaryImageDecoder.php#L37-L73","documentation":"BinaryImageDecoder::decode received a valid string-typed input, but after casting it is the empty string ''. There is nothing to decode, so an InvalidArgumentException is raised immediately — before any format sniffing. Like the sibling type check, this is an input-contract error, not a corrupt-file error.","triggerScenarios":"Calling decode(''), passing a Stringable object whose __toString() returns '' (e.g. an empty value object), optional request parameters that defaulted to '', or variables from functions that returned an empty string on failure (e.g. a failed ob_get_clean or a remote fetch that returned an empty body).","commonSituations":"Empty file uploads (user submitted the form without choosing a file); upstream service returning 200 with an empty body; nullable fields cast to string; test harnesses iterating over fixtures where one fixture file is missing and file_get_contents warning was suppressed.","solutions":["Check for empty input at the boundary: if ($value === '' || $value === null) reject with a user-facing validation error","For uploads, verify size: $request->file('avatar')->getSize() > 0 or isValid()","Fix the producer of the empty string (the failed fetch/ob_get_clean) rather than special-casing the decoder call","Make fields required in form/request validation so empty payloads never reach image processing"],"exampleFix":"// before\n$image = $manager->read($request->input('avatar_base64') ?? '');\n// InvalidArgumentException: Unable to decode binary data from empty string\n\n// after\n$data = (string) $request->input('avatar_base64', '');\nif ($data === '') {\n    return back()->withErrors(['avatar_base64' => 'Avatar upload is required.']);\n}\n$image = $manager->read($data);","handlingStrategy":"validation","validationCode":"$data = (string) $input;\nif ($data === '') {\n    throw new InvalidArgumentException('Image data must not be empty');\n}","typeGuard":"function isNonEmptyBinary(mixed $input): bool\n{\n    return (is_string($input) || $input instanceof \\Stringable) && (string) $input !== '';\n}","tryCatchPattern":null,"preventionTips":["Require the upload field in form/request validation","Check file size > 0 before processing uploads","Guard empty strings where values are fetched (nullable fields default to '')"],"tags":["empty-string","invalid-argument","validation","decoder","binary-data"],"backgroundTag":"empty-string-argument","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}