base64_encode conflict with convert_smilies in wordpress

Here’s the code from formatting.php

if ( get_option( 'use_smilies' ) && ! empty( $wp_smiliessearch ) ) {
    // HTML loop taken from texturize function, could possible be consolidated.
    $textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // Capture the tags as well as in between.
    $stop    = count( $textarr ); // Loop stuff.

The problem is preg_split is failing. Its documentation says it returns false on failure, but the code here isn’t checking for that and feeding the result straight into count(). I’m not sure how to get more details of the preg_split failure. And arguably WordPress should cope with that and skip the smiley step here.

However I’d guess it’s because your encoded image is huge and that’s causing problems in general. Your simplest option is probably to disable convert_smilies(), either by clearing the use_smilies option or removing the filter

remove_filter( 'the_content', 'convert_smilies', 20 );

but I think your best bet would be to handle the image differently: ideally don’t keep it in memory if it’s large – stream it to disk and then fetch the image from an endpoint that reads that back in. However I appreciate that might get complicated quickly, depending on the page flow here: whether this page makes the web service call you’re now trying to separate out into a separate request + so who owns the completion of that call, protecting / cleaning up the images downloaded, potentially sharing them between a cluster of WordPress servers (which might need a separate file share to keep these out of wp_uploads) etc.