How to Get Facebook User’s Profile Picture Using Facebook Graph API

The Facebook API can be used to retrieve a user’s profile picture in different sizes without the need of an access token by sending a GET request to the following url.

Add the optional width and height fields as URL parameters:

https://graph.facebook.com/USER_ID/picture?width=WIDTH&height=HEIGHT

where:

USER_ID is the user id number

WIDTH and HEIGHT are your requested dimension values.

This will return a profile picture with a minimum size of WIDTH x HEIGHT while trying to preserve the aspect ratio. For example,

https://graph.facebook.com/USER_ID/picture?width=140&height=110

returns

{
   "data": {
     "url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/c0.19.180.142/s148x148/2624_134501175351_4831452_a.jpg",
     "width": 140,
     "height": 110,
     "is_silhouette": false
   }
}

To get a user profile picture of a specific size, call

https://graph.facebook.com/USER_ID/picture?type=SIZE

where SIZE should be replaced with one of the defined values square, small, normal or large depending on the size you want.

This call will redirect you to a single image with its size based on your chosen type parameter.

For example:

https://graph.facebook.com/USER_ID/picture?type=small

returns a URL to a small version of the image.

The API only specifies the maximum size for profile images, not the actual size.

Square:

maximum width and height of 50 pixels.

Small:

maximum width of 50 pixels and a maximum height of 150 pixels.

Normal:

maximum width of 100 pixels and a maximum height of 300 pixels.

Large:

maximum width of 200 pixels and a maximum height of 600 pixels.

If you call the default USER_ID/picture you get the square type.

CLARIFICATION

If you call (as per above example)

https://graph.facebook.com/USER_ID/picture?width=140&height=110

it will return a JSON response if you’re using one of the Facebook SDKs request methods. Otherwise, it will return the image itself. To always retrieve the JSON object so you can parse the response based on your needs, add:

&redirect=false

like so:

https://graph.facebook.com/redbull/picture?width=140&height=110&redirect=false

To return just the url of the image, I used the following CURL request in PHP.

function getProfilePic( $userId ) {
  $curl = curl_init();
  $url = "http://graph.facebook.com/". $userId ."/picture?width=100&height=100&redirect=false";
  curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET"
  ));

  $response = curl_exec($curl);

  curl_close($curl);

  $image = json_decode($response, true);

  if ($image['data']['url']) {
    return $image['data']['url'];
  } else {
    return '';
  }

}

This guide has been modified which originally appears in Stackoverflow.

Cromwell Bayon

He is a self-tutored programmer and a Full-Stack Developer. He strives to excel in the newest technology as possible. He has a very high sense of technicality and analytical skills which allows him to resolve any kind of issues related to technology. He also loves woodworking. Read more about him here...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.