James Tang
James Tang

Reputation: 995

Graph API: Retrieving picture dimensions in page feed

I'm using Graph API to get the wall post of a fan page.

I noticed the Facebook iOS app itself is able to determine the exact ratio for its placeholder.

enter image description here

http://developers.facebook.com/tools/explorer?method=GET&path=facebook%2Ffeed%3Ffields%3Dpicture

{
    "data": [
             {
             "picture": "http://photos-b.ak.fbcdn.net/hphotos-ak-frc1/247019_457846347622804_946521095_s.jpg",
             "id": "20531316728_10151883063741729",
             "created_time": "2013-05-02T16:57:25+0000"
             },
               :
               :
               :
             {
             "picture": "http://photos-e.ak.fbcdn.net/hphotos-ak-ash4/223257_10151498310776729_930531604_s.jpg", 
             "id": "20531316728_10151498193061729", 
             "created_time": "2012-10-05T18:42:38+0000"
             }
             ], 
    "paging": {
        "previous": "https://graph.facebook.com/20531316728/feed?fields=picture&limit=25&since=1367513845", 
        "next": "https://graph.facebook.com/20531316728/feed?fields=picture&limit=25&until=1349462557"
    }
}

There it contains no information of the pictures' dimension in the API response, which I'd like to have a correct placeholder size with my custom client like the Facebook iOS app.

I tried adding /facebook/feed?fields=picture,width,height but no luck in retrieving the corresponding information.

Is there a possible way to retrieve the pictures's height and width param from the API itself?

Upvotes: 7

Views: 3936

Answers (2)

林果皞
林果皞

Reputation: 7793

No, API doesn't return ALL picture's dimensions based on post feed from /page_id/feed or stream table.

I mentioned all because, what you can do is:

SELECT post_id, created_time, actor_id, attachment, message FROM stream WHERE source_id=PAGE_ID AND created_time<now() LIMIT 50

If the post type is photo: enter image description here

If the post type is video: enter image description here

If the post type is link start with fbexternal (extract parameter w and h): enter image description here

If the post type is link without fbexternal (facebook photo), we stuck here!: enter image description here

Code Prototype:

if attachment:
    image_url = attachment.media[0].src
    if image_url:
        m = image_url.photo.images
        if m:
            if m.src == image_url:
                image_width = m.width
                image_height = m.height
        else: #no dimensions info
            if host by fbexternal:
                #extract parameter w and h from https://fbexternal-a.akamaihd.net/safe_image.php?d=AQBhfbzbNudc_SE8&w=130&h=130&url=http%3A%2F%2F
                image_width = 130
                image_height = 130
             else:
                 Stuck here, very hard to get extract photo id based on this url, a lot of work!

In conclusion, i highly recommend you to get image width and height after download photo. For example, PHP have getimagesize function and python have Python Imaging Library (PIL).

Upvotes: 1

Zen Leow
Zen Leow

Reputation: 91

I'm not sure if there's an easier way of getting the width and height you need but here's a sure but kinda long-winded way.

Taking your example, the ID that you see for the first picture "20531316728_10151883063741729" isn't actually the ID for the picture. It's for the post that the picture is in.

What you can do, is query for that postID

http://developers.facebook.com/tools/explorer?method=GET&path=20531316728_10151883063741729

then it will expose the picture's object_id 457846347622804 (do a search and you'll see what I mean). now you query for that picture's ID and you'll have your width and height served.

http://developers.facebook.com/tools/explorer?method=GET&path=457846347622804

From then on you should be able to get the ratio you need.

If anyone has an elegant way of getting this done, I'd love to know too.

Upvotes: 0

Related Questions