json_encode fails when picture binary data present

The place for "I can't figure out how to..." questions.
Post Reply
thefeldkircher
User
Posts: 3
Joined: Mon Oct 13, 2014 1:39 pm
Are you a spambot?: no

json_encode fails when picture binary data present

Post by thefeldkircher » Mon Oct 13, 2014 2:12 pm

Hi All

Hope someone can help me....

I'm using the following code to build an array of specific tags, once complete this array is json_encoded and returned to my app.

Code: Select all

elseif(is_readable("$dir$entry")) { 
	$trackInfo = $getID3->analyze("$dir$entry");
			
	if(!empty($trackInfo["comments"]["picture"][0]['data'])){
		$encoded = base64_encode($trackInfo["comments"]["picture"][0]['data']);
		//echo $encoded;
	}
			
	$retval[] = array(
		"track" => array(
			"filepath" => $trackInfo["filepath"],
			"filename" => $trackInfo["filename"],
			"filenamepath" => $trackInfo["filenamepath"],
			"tags_html" => $trackInfo["tags_html"],
			"playtime_string" => $trackInfo["playtime_string"],
			"picturedata" => (!empty($trackInfo["comments"]["picture"])) ? $encoded : null,
			"mimetype" => (!empty($trackInfo["comments"]["picture"])) ? $trackInfo["comments"]["picture"][0]['image_mime'] : null
			)
		);
	}
      } 
    $d->close(); 
		
    return $retval; 
} 

$dirlist = getFileList("music", true);
However, when 'picturedata' is present with or without base64 encoding the following fails.

Code: Select all

echo ")]}',\n" . json_encode($dirlist);
")]}',\n" is just angularjs json-csrf

How can I get picture binary data as a json_encoded element?

Thanks in advance

James Heinrich
getID3() v1 developer
Posts: 1411
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada
Contact:

Re: json_encode fails when picture binary data present

Post by James Heinrich » Mon Oct 13, 2014 3:01 pm

JSON doesn't support binary data. You'd need to do something like base64-encode data before JSON-encoding it, then base64-decode afterwards.

In any case, not really a getID3 issue.

thefeldkircher
User
Posts: 3
Joined: Mon Oct 13, 2014 1:39 pm
Are you a spambot?: no

Re: json_encode fails when picture binary data present

Post by thefeldkircher » Mon Oct 13, 2014 3:56 pm

Thanks for quick response, as you can see from my sample code I'm using base64_encode and it still fails. Is there an alternative method of getting the data in another format

James Heinrich
getID3() v1 developer
Posts: 1411
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada
Contact:

Re: json_encode fails when picture binary data present

Post by James Heinrich » Mon Oct 13, 2014 4:14 pm

Sorry, I missed that you already had base64 encoding going on.
From the little I know about json_encode it would seem to me that it should work. Have you checked what json_last_error says after it fails to encode?

thefeldkircher
User
Posts: 3
Joined: Mon Oct 13, 2014 1:39 pm
Are you a spambot?: no

Re: json_encode fails when picture binary data present

Post by thefeldkircher » Mon Oct 13, 2014 4:29 pm

Yes, I'm pulling individual array indexes and running it through a verifier - no errors on indexes without binary data, but no error and no output with binary data.

OK, this as gone into php side of things, so thanks for your help. If I ever solve this I'll post an answer here.

Thanks for your help.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest