{"record":{"id":"ae26512050d2f88e","repo":"Intervention/image","slug":"failed-to-rewind-position-of-stream","errorCode":null,"errorMessage":"Failed to rewind position of stream","messagePattern":"Failed to rewind position of stream","errorType":"exception","errorClass":"StreamException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/Decoders/StreamImageDecoder.php","lineNumber":50,"sourceCode":"     *\n     * @throws InvalidArgumentException\n     * @throws StreamException\n     * @throws DriverException\n     * @throws StateException\n     * @throws ImageDecoderException\n     * @throws NotSupportedException\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        $result = rewind($input);\n\n        if ($result === 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 {\n            return parent::decode($contents);\n        } catch (DecoderException) {\n            throw new ImageDecoderException(\n                'Failed to decode image from stream, could be unsupported image format',\n            );\n        }","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/Decoders/StreamImageDecoder.php#L32-L68","documentation":"Before reading, the decoder rewinds the stream to its start; rewind() returning false means the stream is not seekable or has no position to return to. This StreamException reports a capability of the stream, not anything about the image data.","triggerScenarios":"Passing fopen('http://example.com/img.png') (HTTP wrapper, non-seekable), a pipe from popen/proc_open, or a stream whose seeking is unsupported because the underlying transport cannot reposition.","commonSituations":"Reading remote images directly from the HTTP stream wrapper, piping image bytes from a subprocess, consuming php://input in setups where it cannot be rewound.","solutions":["Buffer the source into a seekable php://temp stream and pass that","Or read the full contents to a string and call $manager->read($contents)","For remote files, fetch with an HTTP client first, or pass the URL so the library handles buffering","Check stream_get_meta_data($fp)['seekable'] before calling decodeStream()"],"exampleFix":"// before\n$image = $manager->decodeStream(fopen('https://example.com/img.png', 'rb')); // not seekable\n\n// after\n$buffer = fopen('php://temp', 'rb+');\nstream_copy_to_stream(fopen('https://example.com/img.png', 'rb'), $buffer);\nrewind($buffer);\n$image = $manager->decodeStream($buffer);\n// or: $image = $manager->read(file_get_contents('https://example.com/img.png'));","handlingStrategy":"validation","validationCode":"$meta = stream_get_meta_data($fp);\nif (!$meta['seekable']) {\n    $buffer = fopen('php://temp', 'rb+');\n    stream_copy_to_stream($fp, $buffer);\n    rewind($buffer);\n    $fp = $buffer;\n}\n$manager->decodeStream($fp);","typeGuard":null,"tryCatchPattern":"try {\n    $image = $manager->decodeStream($fp);\n} catch (StreamException $e) {\n    $image = $manager->read(stream_get_contents($fp, offset: 0) ?: '');\n}","preventionTips":["Always buffer remote/pipe streams into php://temp before decoding","Check stream_get_meta_data()['seekable'] for unfamiliar streams","Fetch remote files with an HTTP client rather than the stream wrapper"],"tags":["stream","seekable","http-stream","gd"],"backgroundTag":"stream-not-seekable","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}