I created an object from PHP with json encode, but I am having difficulty using the data to get the image width and height in the object.
This is how I created the objects:
$get = mysql_query("SELECT x,y,img,bid FROM player WHERE uid='1'");
while($row = mysql_fetch_assoc($get)){
$data[$row['x']] = Array();
$data[$row['x']][$row['y']][0] = $row['bid']; // 1
$data[$row['x']][$row['y']][1] = $row['img']; // test.png
}
$data1 = json_encode($data);
See comments for test data used.
I assign this $data1 to variable sdata.
Now in my JavaScript I am trying to do this:
imgwidth = sdata[i][j][1].width;
imgheight = sdata[i][j][1].height;
But I get undefined, is there something I am missing out ?
If I read this correctly,
sdata[i][j][1]in JS will map to the string"test.png". That is, the JSON is encoding the filename of the image, not the image data itself. Sosdata[i][j][1].widthis asking for thewidthproperty of a string"test.png", which doesn’t exist.You’ll probably need to open the image on the server side (using ImageMagick or GD in PHP) and pull out the properties from there.
Have a look at the getimagesize() function as a starting point.