How to get large photo URL in one API call?

匆匆过客 提交于 2019-12-02 18:47:17

I haven't tried this with stream/feed photos, but the generally accepted way of doing this is:

http://graph.facebook.com/{ID of object}/picture

If you want the "large" version, you would do:

http://graph.facebook.com/{ID of object}/picture?type=large

I'm not 100% sure if this would work for an actual photo (instead of a user profile picture or page profile pic), but I have a hunch it will - the only caveat is that you obviously must have a logged in user that is authorized to view the photo (unless it's public).

If anybody is looking to this and type large is not enough, I found other solutions.

Type large is kind of small anyway (close to 200px). You can get larger image by adding i.e. ?width=1000 or ?height=1000. Facebook will return picture closest to given dimension and preserve aspect ratio. When passing both dimenstions like ?width=1000&height=1000, facebook will cut image to given dimensions (in this case square).

Use Facebook UserId (Oject ID) to get the picture.

https://graph.facebook.com/173xxxx8635/picture?type=large&redirect=false

which returns JSON data with picture URL.

{
   "data": {
      "is_silhouette": false,
      "url": "https://fbcdn-profile-a.akamaihd.net/xxx/xyz/1cc066a2cae3f301d"
   }
}

A good trick with the new api is to get the pic_cover field from the event table and to process it according to the size you want to use

I found when I was having this trouble that it turned out to be the picture I was downloading rather than the size I was setting it.

If for example I downloaded all my photos with a request of

[FBRequestConnection startWithGraphPath:@"/me/photos?fields=created_time,name,picture&type=tagged" parameters:nil HTTPMethod:@"GET" completionHandler:^(FBRequestConnection * connection, id result, NSError *error) {

    NSDictionary * userData = (NSDictionary *)result;
    NSMutableArray * array = [[NSMutableArray alloc] initWithArray:userData[@"data"]];

    for (NSDictionary * dict in eventsToAdd) {

        UIImage * image = dict[@"picture"] 
    }
}];

I am using the dictionary key search "picture" as I want the picture.

This though will get me a lower quality picture than if I searched for "source" in this search:

[FBRequestConnection startWithGraphPath:@"/me/photos?fields=created_time,name,source&type=tagged" parameters:nil HTTPMethod:@"GET" completionHandler:^(FBRequestConnection * connection, id result, NSError *error) {

    NSDictionary * userData = (NSDictionary *)result;
    NSMutableArray * array = [[NSMutableArray alloc] initWithArray:userData[@"data"]];

    for (NSDictionary * dict in eventsToAdd) {

        UIImage * image = dict[@"source"] 
    }
}];

If you go on the Facebook API explorer and search for photos and then click on the picture and source jpg links you can see the difference in size and quality.

Since changing this method I have managed to get rid of using the type parameters as it doesn't seem to make a different.

Note: I am using iPhone and not iPad or a larger screen so I don't know how this affects bigger screens.

The answer by @streetlogics works fine but only on pictures that have {object_id}.

http://graph.facebook.com/{object_id}/picture

But I also wanted large pictures for the feed's shared links, which sometimes don't have {object_id}. I finally realized that the {picture} thumbnail URL contains the encoded URL for the original site's large image:

https://external.xx.fbcdn.net/safe_image.php?d=AQBe9UvGd0vPbAHP&w=130&h=130&url=http%3A%2F%2Fskift.com%2Fwp-content%2Fuploads%2F2015%2F12%2Fpollution.jpg&cfs=1

--> contains -->

http://skift.com/wp-content/uploads/2015/12/pollution.jpg

So I made a loop that checks for {object_id} and if not present then extracts the URL from {picture}:

if(isset($post['object_id'])) {
    echo "http://graph.facebook.com/".$post['object_id']."/picture";
    }
    elseif(isset($post['picture'])) {
        echo urldecode(preg_replace('/&cfs.*/', '', preg_replace('/.*url=/', '', $post['picture'])));
    } 
    else {
        echo "no_large_image";
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!