Short description: I query my PHP
back-end which returns rows of data from an SQL
database, the data is returned in JSON
format. The data includes a link to an image for each row, to be able to create a placeholder I also determine the width and height of the image by reading 32KB of image data, using the "ranger" function as per webarto's answer here and then append this data to my resulting JSONArray
.
This is the the code I have:
$result = $conn->query ( $sql );
$response = array ();
if ($result->num_rows > 0) {
while ( $row = $result->fetch_assoc () ) {
/* get image url */
$url = $row ['image'];
/* get the first 32KB of image data */
$raw = ranger ( $url );
/* recreate a part of the original image */
$im = imagecreatefromstring ( $raw );
/* determine width and height of the image */
$width = imagesx ( $im );
$height = imagesy ( $im );
/* add width and height at the end of the array */
array_push ( $row, $width );
array_push ( $row, $height );
$response [] = $row;
}
$j_result = json_encode ( $response );
echo $j_result;
Which gives the following result for one row:
{"lang":"de","title":"Sample Title","article":"Sample text","time":"1416070535","image":"someimage.jpg","source":"sample.com","0":606,"1":379}
I'm not really familiar with PHP
and managed to append the image dimensions using the array_push()
function. However, this gives me "0", "1" etc. as the key for those values as you can see in the JSON
above.
The question: how can I set those keys so json_encode
does not set them automatically?