mardi 28 avril 2015

How to encode images to be used in an JSON array?

I want to do the following:

  1. Connect to Amazon S3 and get an image (.jpg) or sound (.m4a).
  2. Then I want to put them in an array of objects.
  3. Send them to an client

The file exsists in the S3 and can be reached with an browser.

Step one is already done with the following:

try{
    $result = $client->getObject(array(
        'Bucket' => $bucket,
        'Key'    => $filename
    ));
} catch (Exception $e) { //ERROR
    echo($e->getMessage());// FOR THE ERROR
}

I use the $result['Body'] to get the image.

Note: the server is an EC2 instance so the password is already done with an role from IAM

Step two:

image_array=[image1,sound1,sound2,image2];

echo json_encode(image_array);

This step is giving me an empty array. I understand that it is empty because of the encoding. The images are binary data objects and will not work fine with the JSON. But what is the right way? Should I do something like

image_array=[json_encode(image1),etc.];

Or should I do something like this

image_array=[utf8_encode(image1),etc];

Question: How am I supposed to give the image back in an JSON code so I won't break and is readable?

Note: I use this to give info back to the client something similar to this

total_array=[ [image1,property1,property2],
              [image2,property1,property2],
              [image3,property1,property2],
];




Aucun commentaire:

Enregistrer un commentaire