{"record":{"id":"0bf6af5a7114b99d","repo":"Intervention/image","slug":"image-source-must-be-a-resource-of-type-file-or-0bf6af","errorCode":null,"errorMessage":"Image source must be a resource of type \"file\" or \"stream\"","messagePattern":"Image source must be a resource of type \"file\" or \"stream\"","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Decoders/StreamImageDecoder.php","lineNumber":40,"sourceCode":"    public function supports(mixed $input): bool\n    {\n        return is_resource($input);\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see DecoderInterface::decode()\n     *\n     * @throws InvalidArgumentException\n     * @throws StreamException\n     * @throws DriverException\n     * @throws StateException\n     */\n    public function decode(mixed $input): ImageInterface\n    {\n        if (!is_resource($input) || !in_array(get_resource_type($input), ['file', 'stream'])) {\n            throw new InvalidArgumentException('Image source must be a resource of type \"file\" or \"stream\"');\n        }\n\n        $contents = '';\n        $rewind = rewind($input);\n        if ($rewind === false) {\n            throw new StreamException('Failed to rewind position of stream');\n        }\n\n        while (!feof($input)) {\n            $chunk = fread($input, 1024);\n            if ($chunk === false) {\n                throw new StreamException('Failed to read image from stream');\n            }\n\n            $contents .= $chunk;\n        }\n\n        try {","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Decoders/StreamImageDecoder.php#L22-L58","documentation":"StreamImageDecoder::decode() requires a PHP resource whose get_resource_type() is 'file' or 'stream'. Passing any other resource type - or an already-closed resource (is_resource() returns false after fclose) - fails here. Usually hit by calling the decoder directly or by confusing a stream handle with a stream wrapper URL string.","triggerScenarios":"Passing a cURL handle (curl_init()), a popen() pipe, or a resource closed elsewhere; passing the string 'php://temp' instead of an fopen() handle; passing a PSR-7 StreamInterface object instead of $stream->detach().","commonSituations":"Refactors from string paths to streams; integrating with PSR-7 request/response bodies; consuming resources produced by curl multi handles.","solutions":["Open a real handle first: $manager->read(fopen($path, 'rb'))","For PSR-7 bodies, pass the underlying resource: $manager->read($stream->detach())","Guard with is_resource($h) && in_array(get_resource_type($h), ['file', 'stream']) before passing"],"exampleFix":"// before\n$image = $manager->read('php://temp'); // string, not a resource\n\n// after\n$handle = fopen('php://temp', 'r+b');\nfwrite($handle, $imageBytes);\n$image = $manager->read($handle);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function isFileStreamResource(mixed $value): bool\n{\n    return is_resource($value)\n        && in_array(get_resource_type($value), ['file', 'stream'], true);\n}","tryCatchPattern":null,"preventionTips":["Open handles with fopen() before passing; never pass URL strings or curl handles","For PSR-7 streams pass ->detach(), and check is_resource() afterwards (detach may be one-shot)"],"tags":["stream","resource","type-error","invalid-argument"],"backgroundTag":"invalid-argument-type","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}