{"record":{"id":"25726c03b4a8bcee","repo":"Intervention/image","slug":"image-source-must-be-binary-data-of-type-string-or","errorCode":null,"errorMessage":"Image source must be binary data of type string or instance of Stringable","messagePattern":"Image source must be binary data of type string or instance of Stringable","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/Decoders/BinaryImageDecoder.php","lineNumber":47,"sourceCode":"    {\n        return $this->couldBeBinaryData($input);\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see DecoderInterface::decode()\n     *\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","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/Decoders/BinaryImageDecoder.php#L29-L65","documentation":"Contract violation of BinaryImageDecoder::decode: the input is neither a PHP string nor a Stringable object, so it cannot be raw image binary. This InvalidArgumentException is a programmer-error guard — normal manager routing (supports() via couldBeBinaryData) already filters non-strings, so hitting it means the decoder was called directly or with an unexpected value type.","triggerScenarios":"Calling BinaryImageDecoder (or a subclass like Base64ImageDecoder) ->decode() directly with null, an int, an array, a PSR-7 stream, or an SplFileObject; passing a resource from file_get_contents(..., use include path) style calls that can return false/null; untyped mixed values flowing from decoded JSON into decoder calls.","commonSituations":"Custom decoder chains wired manually into a factory; refactorings that changed an upstream return type from string to object/null; optional fields (nullable string) passed without a null check; unit tests exercising the decoder with scalar literals.","solutions":["Cast or stringify the value before calling: (string) $value, or fetch stream contents with (string) $stream","Add a type guard: is_string($input) || $input instanceof Stringable before invoking decode","Make the calling code's signature honest (string $input instead of mixed) so PHP itself rejects bad types earlier","If you meant to read a file path, stream, or GdImage, route through ImageManager::read() instead of the binary decoder directly"],"exampleFix":"// before\n$decoder = new BinaryImageDecoder();\n$image = $decoder->decode($request->file('avatar')); // UploadedFile object\n// InvalidArgumentException: Image source must be binary data ...\n\n// after\n$image = $manager->read($request->file('avatar')->getContent());\n// or let the manager auto-route: $manager->read($request->file('avatar')->getRealPath())","handlingStrategy":"type-guard","validationCode":"if (!is_string($input) && !$input instanceof \\Stringable) {\n    throw new InvalidArgumentException('Expected binary string, got ' . get_debug_type($input));\n}","typeGuard":"/**\n * @param mixed $input\n */\nfunction isBinaryLike(mixed $input): bool\n{\n    return is_string($input) || $input instanceof \\Stringable;\n}","tryCatchPattern":"try {\n    $image = $decoder->decode($input);\n} catch (\\Intervention\\Image\\Exceptions\\InvalidArgumentException $e) {\n    // programmer error: fix the call site, do not catch-and-continue in production\n    throw $e;\n}","preventionTips":["Prefer ImageManager::read() over direct decoder calls so routing is automatic","Type your variables as string/Stringable upstream instead of mixed","TurnUploadedFile/stream inputs into strings with getContent()/(string) $stream first"],"tags":["type-error","invalid-argument","decoder","binary-data","php"],"backgroundTag":"invalid-argument-type","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}