Using the stream filter doesn't make much sense here. You're not saving any memory by letting a stream filter do the encoding, rather than base64 encode, because the input (`$data`) and the output (the `data://` url) are still strings.

However, i think the reason the stream filter is failing, is because you probably need to use `STREAM_FILTER_WRITE` instead.