{"record":{"id":"d76ce77d9baf67dd","repo":"Intervention/image","slug":"base64-encoded-data-must-be-either-of-type-string","errorCode":null,"errorMessage":"Base64-encoded data must be either of type string or instance of Stringable","messagePattern":"Base64-encoded data must be either of type string or instance of Stringable","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Drivers/AbstractDecoder.php","lineNumber":81,"sourceCode":"        } finally {\n            if (is_resource($source)) {\n                fclose($source);\n            }\n        }\n\n        return new Collection(is_array($data) ? $data : []);\n    }\n\n    /**\n     * Decodes given base64 encoded data.\n     *\n     * @throws InvalidArgumentException\n     * @throws DecoderException\n     */\n    protected function decodeBase64Data(mixed $input): string\n    {\n        if (!is_string($input) && !$input instanceof Stringable) {\n            throw new InvalidArgumentException(\n                'Base64-encoded data must be either of type string or instance of Stringable',\n            );\n        }\n\n        $decoded = base64_decode((string) $input, true);\n\n        if ($decoded === false) {\n            throw new DecoderException('Input is not valid Base64-encoded data');\n        }\n\n        if (base64_encode($decoded) !== str_replace([\"\\n\", \"\\r\"], '', (string) $input)) {\n            throw new DecoderException('Input is not valid Base64-encoded data');\n        }\n\n        return $decoded;\n    }\n}\n","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/AbstractDecoder.php#L63-L99","documentation":"decodeBase64Data() in AbstractDecoder accepts only a string or a Stringable object carrying Base64-encoded image data. The guard runs before any decoding, so arrays, null, integers, resources, and objects without __toString() are rejected immediately with this InvalidArgumentException.","triggerScenarios":"Calling the base64 decoding path (read()/decode with base64 input) with a value that came out of json_decode as an array, a null from a missing request field, or a stdClass without __toString().","commonSituations":"Untyped request payloads fed straight to ImageManager::read(); optional upload fields that are null when omitted; passing a PSR-7 stream object where a string is expected.","solutions":["Verify the value is a string (or cast it) before passing: is_string($v) || $v instanceof Stringable","Null-check optional fields and skip the read when empty","Extract the body first when the source is a stream object: (string) $stream->getContents()"],"exampleFix":"// before\n$image = $manager->read($request->input('image'));\n\n// after\n$data = $request->input('image');\n$image = is_string($data) || $data instanceof \\Stringable\n    ? $manager->read($data)\n    : throw new \\RuntimeException('Missing image data');","handlingStrategy":"type-guard","validationCode":"if (!is_string($data) && !$data instanceof \\Stringable) {\n    throw new \\InvalidArgumentException('Image data must be stringable');\n}","typeGuard":"function isStringable(mixed $value): bool\n{\n    return is_string($value) || $value instanceof \\Stringable;\n}","tryCatchPattern":null,"preventionTips":["Validate untyped request values before read()","Null-check optional upload fields","Cast stream bodies to string explicitly"],"tags":["base64","decoder","type-check","input-validation"],"backgroundTag":"invalid-argument-type","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}