Base64 encoding works by expanding blocks of 3 bytes of data into blocks of
4 bytes of data. Finally it also includes a well defined ending of the
encoded data to make sure the size of the final block of encoded data is 4
bytes too.

Documentation

This function encodes as large a portion of the input as possible and
returns the encoded part together with the remaining part. Enough space is
allocated for the encoding to make sure that the remaining part is less than
3 bytes long, which means it can be passed to b64_encode_final as is.