{"record":{"id":"67ca2d7bc6be9e4e","repo":"Intervention/image","slug":"image-source-must-be-binary-data-of-type-string-or-67ca2d","errorCode":null,"errorMessage":"Image source must be binary data of type string or instance of ","messagePattern":"Image source must be binary data of type string or instance of ","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Decoders/BinaryImageDecoder.php","lineNumber":45,"sourceCode":"    public function supports(mixed $input): bool\n    {\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     */\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","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Decoders/BinaryImageDecoder.php#L27-L63","documentation":"Thrown by BinaryImageDecoder::decode() when the input is neither a PHP string nor a Stringable object. The binary decoder only accepts raw binary image data as string(-like) input; resources, objects, arrays, or null are rejected before any decoding starts.","triggerScenarios":"Directly or via routing, calling $manager->read($x) where $x is a file handle from fopen(), a GdImage/Imagick object, an array, null from a failed lookup, or a stream resource from an HTTP client — on the code path where the binary decoder ends up handling it (e.g. a custom decoder chain or calling the decoder directly).","commonSituations":"Passing fopen('img.png', 'r') instead of file_get_contents(); passing a Guzzle PSR-7 stream; passing null after an optional request field was absent; Stringable value objects elsewhere in the app making developers assume any object works.","solutions":["Convert resources to strings first: stream_get_contents($resource) or (string) $response->getBody()","For files prefer $manager->read($path) with the filepath decoder, or read the bytes with file_get_contents","Null-check optional inputs before calling read()"],"exampleFix":"// before: resource passed as image source\n$image = $manager->read(fopen($path, 'r'));\n\n// after: string of bytes\n$image = $manager->read(file_get_contents($path));\n// or let the filepath decoder work:\n$image = $manager->read($path);","handlingStrategy":"type-guard","validationCode":"if (!is_string($input) && !$input instanceof Stringable) {\n    throw new InvalidArgumentException('image source must be a string of bytes');\n}\n$image = $manager->read($input);","typeGuard":"function isBinaryLike(mixed $value): bool\n{\n    return is_string($value) || $value instanceof Stringable;\n}","tryCatchPattern":"use Intervention\\Image\\Exceptions\\InvalidArgumentException;\n\ntry {\n    $image = $manager->read($input);\n} catch (InvalidArgumentException $e) {\n    // fix the caller: cast resources/streams to string before passing\n    throw $e;\n}","preventionTips":["Convert streams/resources with stream_get_contents() before read()","Null-check optional inputs at the boundary (form request validation)","Type-hint string|Stringable in your own wrappers"],"tags":["input-validation","type-error","decoding","string"],"backgroundTag":"invalid-image-input-type","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}