{"record":{"id":"f951f747050f6c2c","repo":"Intervention/image","slug":"unable-to-decode-binary-data-from-empty-string-f951f7","errorCode":null,"errorMessage":"Unable to decode binary data from empty string","messagePattern":"Unable to decode binary data from empty string","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Decoders/BinaryImageDecoder.php","lineNumber":53,"sourceCode":"     * @see DecoderInterface::decode()\n     *\n     * @throws InvalidArgumentException\n     * @throws ImageDecoderException\n     * @throws DriverException\n     * @throws StateException\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        try {\n            $imagick = new Imagick();\n            $imagick->readImageBlob($input);\n        } catch (ImagickException) {\n            throw new ImageDecoderException('Failed to decode unsupported image format from binary data');\n        }\n\n        // decode image\n        $image = parent::decode($imagick);\n\n        // get media type enum from string media type\n        $format = Format::tryCreate($image->origin()->mediaType());\n\n        // extract exif data for appropriate formats\n        if (in_array($format, [Format::JPEG, Format::TIFF])) {\n            $image->setExif($this->extractExifData($input));","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Decoders/BinaryImageDecoder.php#L35-L71","documentation":"Thrown by BinaryImageDecoder::decode() when the input string is empty (''). After the Stringable/string cast, a zero-length payload cannot contain an image, so it is rejected before Imagick is invoked. Distinct from decode failures: it means there was no data at all.","triggerScenarios":"$manager->read(''), or reading a variable that resolves to '' — an empty uploaded file (0-byte $_FILES entry), file_get_contents on an empty/failed fetch returning false-cast-to-string, trim()ed user input, or a Stringable whose __toString returns ''.","commonSituations":"Upload forms submitted without a chosen file; upstream fetch returning an empty body that is passed through; queue payloads where the image field was never set; tests feeding '' as a quick placeholder.","solutions":["Reject empty input before reading: if ($input === '') fail with a validation message","For uploads, check $_FILES['file']['error'] === UPLOAD_ERR_OK and size > 0","Treat empty fetch bodies as an error rather than passing them on"],"exampleFix":"// before\n$image = $manager->read($request->file('avatar')->getContent()); // '' when upload empty\n\n// after\n$file = $request->file('avatar');\nabort_if($file === null || $file->getSize() === 0, 422, 'Image required');\n$image = $manager->read($file->getContent());","handlingStrategy":"validation","validationCode":"if ($input === null || trim((string) $input) === '') {\n    throw new InvalidArgumentException('image data is empty');\n}\n$image = $manager->read((string) $input);","typeGuard":"function isNonEmptyString(mixed $value): bool\n{\n    return is_string($value) && $value !== '';\n}","tryCatchPattern":"use Intervention\\Image\\Exceptions\\InvalidArgumentException;\n\ntry {\n    $image = $manager->read($input);\n} catch (InvalidArgumentException $e) {\n    return back()->withErrors(['image' => 'empty upload']);\n}","preventionTips":["Require non-empty uploads at the validation layer (size > 0, UPLOAD_ERR_OK)","Treat empty fetch bodies as transport errors, not image input","Never pass optional request fields to read() without a presence check"],"tags":["input-validation","empty-input","decoding","upload"],"backgroundTag":"empty-image-input","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}