title: How to encode images to be used in an JSON array?
body: <p>I want to do the following:</p>
  
  <ol>
  <li>Connect to Amazon S3 and get an image (.jpg) or sound (.m4a).</li>
  <li>Then I want to put them in an array of objects.</li>
  <li>Send them to an client</li>
  </ol>
  
  <p>The file exsists in the S3 and can be reached with an browser.</p>
  
  <p>Step one is already done with the following:</p>
  
  <pre><code>try{
      $result = $client-&gt;getObject(array(
          'Bucket' =&gt; $bucket,
          'Key'    =&gt; $filename
      ));
  } catch (Exception $e) { //ERROR
      echo($e-&gt;getMessage());// FOR THE ERROR
  }
  </code></pre>
  
  <p>I use the $result['Body'] to get the image. </p>
  
  <p><em>Note: the server is an EC2 instance so the password is already done with an role from IAM</em></p>
  
  <p>Step two:</p>
  
  <pre><code>image_array=[image1,sound1,sound2,image2];
  
  echo json_encode(image_array);
  </code></pre>
  
  <p>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 </p>
  
  <pre><code>image_array=[json_encode(image1),etc.];
  </code></pre>
  
  <p>Or should I do something like this </p>
  
  <pre><code>image_array=[utf8_encode(image1),etc];
  </code></pre>
  
  <p><strong>Question: How am I supposed to give the image back in an JSON code so I won't break and is readable?</strong></p>
  
  <p><em>Note: I use this to give info back to the client something similar to this</em></p>
  
  <pre><code>total_array=[ [image1,property1,property2],
                [image2,property1,property2],
                [image3,property1,property2],
  ];
  </code></pre>