• Hi

    I am using PHP code (actually in the snippets plugin though that is not the underlying cause) to show an HTML image:

    $logohtml="<img width='15%' height='15%' src='data:image/".$eventData["LogoType"].";base64,".base64_encode($eventData["Logo"])."'/>";
    $logohtml is then returned from the fucntion called by a shortcode.

    But it generates this error:
    Parameter must be an array or an object that implements Countable in /homepages/14/*****/htdocs/*******/wp-includes/formatting.php on line 3362

    This seems to be the function convert_smilies() code. I found some historic references to this being being a known issue and which could be addressed by disabling smilie conversions but that is not the issue here.

    Tried all sorts of fixes with out success.

    Any ideas?

    Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • Topher

    (@topher1kenobe)

    The only thing function in that code is base64_encode(), which isn’t even a WordPress function. There isn’t any WordPress code in there at all. Are you sure it’s THIS line that’s causing the problem? Often with PHP it’s the line right before the error.

    Thread Starter paulred222

    (@paulred222)

    Thanks for this. The error is in formatting.php which is definitely WordPress code. I think the sequence is that the code is generated/emitted in the snippets plugin but then processed by formatting.php. The function where it fails in this is the one which replaces smilies.

    Dion

    (@diondesigns)

    You have multiple errors in your code. The following code fixes those errors and will hopefully work for you:

    $logohtml = '';
    if (!empty($eventData['Logo'])) {
    	$logomime = $eventData['LogoType'];
    	$logosrc = base64_encode($eventData['Logo']);
    	$logohtml = "<img width='15%' height='15%' src='data:image/{$logomime};base64,{$logosrc}' />";
    }
    Moderator bcworkz

    (@bcworkz)

    We can see that error when the HTML content being processed is badly formed. An explanation of why Dion’s code would resolve a seemingly unrelated error.

    Thread Starter paulred222

    (@paulred222)

    Thanks. I have tried Dion’s code but get exactly the same error. It is strange as I am using almost exactly the same code for the same image in a plugin I wrote invoked via a shortcode, which works perfectly and which gives the same html. In the plugin case the image is in a datatables table.

    So I have:
    Code invoked by shortcode implemented in code snippet. Not in a table. Fails
    Code involved by shortcode implemented in plugin and in a databable. Works fine.

    I wonder if the failing one is due to some other characteristic of the code snippet. I am investigating.

    Thread Starter paulred222

    (@paulred222)

    I am on track to find the underlying cause – it isdata dependent. The image was being read from a database field then encoded using base64_encode($eventData[“Logo”])
    If I tried various other images it all worked fine. But the image causing the problems uploaded fine and was displayed perfectly in the datatables part of the code which still uses base64_encode($eventData[“Logo”]) with an identical img tag.

    Thread Starter paulred222

    (@paulred222)

    This is a WordPress bug or feature. I thought for a time it was due to me storing the raw instead of base64 data in the database which has the image but this is not the case – I changed it to store the image in base64 and it still fails.

    In the code the line with comment //*** changes the logo data read from the database to a hard coded red dot. Works fine. Comment it out to use the picture data read from the database and I get the error.

    But use other images and it is fine. It is definitely an accident of the image data.

    Looking at the code where the error occurrs in formatting.php, this code scans for smilies and I think it is wrongly detecting something in the image data as a smilie tag or something like that. This is WordPress 5.7.2

    $eventData['Logo']="iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; //***
    			$logohtml=""; //get logo if required
    			if ($eventData["Logo"]) {
    					$logohtml='<img width="15%" height="15%" src="data:image/'.$eventData['LogoType'].';base64,'.$eventData['Logo'].'"/>';
    				  //$logohtml='<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />';
    			}
    Moderator bcworkz

    (@bcworkz)

    I agree it’s something about the image data. The actual root cause of the error is the preg_split() call just above where the count() error occurs. The function splits content into parts bounded by HTML tags for further processing. I don’t know why but something in the data causes the function to error out, causing the subsequent count() to complain it can’t count what it has been given.

    The fact hardcoded image data is OK but not from the database causes me to suspect the data wasn’t properly escaped somewhere along its travels. Could even be prior to it being saved in the DB.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Parameter must be an array or an object that implements Countable with img tag’ is closed to new replies.