3

How to detect default avatar on the link like this: https://graph.facebook.com/'.$id.'/picture?type=large? Is it the only way to get avatars (male/female) from special prepared profiles and then compare by e.g. md5() ?

It's hard to believe this is the only way.

hbk
  • 10,908
  • 11
  • 91
  • 124
  • 1
    There's the profile picture of the user, which you already know the url for, what exactly do you need other than that? – Nitzan Tomer May 25 '12 at 14:45

4 Answers4

21

You can use the redirect=false parameter:

https://graph.facebook.com/naitik/picture?redirect=false

Then facebook's responce is json and contains this data:

{
   "data": {
      "url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/157337_5526183_369516251_q.jpg",
      "is_silhouette": false
   }
}

You can use the is_silhouette option to detect if the photo is default one.

You can read more at: https://developers.facebook.com/docs/reference/api/using-pictures/

kachar
  • 2,310
  • 30
  • 32
6

There isn't an API you can call to tell if they are using the default photo or not. Instead of downloading the whole image and checking the MD5, you could issue an HTTP HEAD request to that profile URL and look at the Location header and see if the URL is one of the known default profile images:

Male: https://fbcdn-profile-a.akamaihd.net/static-ak/rsrc.php/v2/yL/r/HsTZSDw4avx.gif https://fbcdn-profile-a.akamaihd.net/static-ak/rsrc.php/v2/yL/r/HsTZSDw4avx.gif

Female (Darth Vader):https://fbcdn-profile-a.akamaihd.net/static-ak/rsrc.php/v2/yp/r/yDnr5YfbJCH.gif

enter image description here

These URL's could change I suppose, and so could the default photos, but I haven't seen either case happen that I can remember.

hbk
  • 10,908
  • 11
  • 91
  • 124
bkaid
  • 51,465
  • 22
  • 112
  • 128
1

If you are already making a call to the Graph API to get user data like the avatar, just include picture in the fields param when you make your first call to Graph API, then the response will include the is_silhouette offset, if it's set to true the user has the default avatar.

Request:

https://graph.facebook.com/v2.7/me?access_token=[token]&fields=name,picture

Response:

{
    "id": "100103095474350",
    "name": "John Smith",
    "picture": {
        "data": {
            "is_silhouette": true,
            "url": "https://scontent.xx.fbcdn.net/v/...jpg"
        }
    }
}
Mitchell McKenna
  • 2,267
  • 15
  • 17
0

Using Facebook SDK for iOS (Swift 4):

class FacebookSignIn {

    enum Error: Swift.Error {
       case unableToInitializeGraphRequest
       case unexpectedGraphResponse
    }

    func profileImageURL(size: CGSize, completion: @escaping Result<URL?>.Completion) {
       guard let userID = FBSDKAccessToken.current()?.userID else {
          completion(.failure(Error.unableToInitializeGraphRequest))
          return
       }
       let params: [String: Any] = ["redirect": 0,
                                    "type": size.width == size.height ? "square" : "normal",
                                    "height": Int(size.height),
                                    "width": Int(size.width),
                                    "fields": "is_silhouette, url"]

       guard let request = FBSDKGraphRequest(graphPath: "/\(userID)/picture", parameters: params) else {
          completion(.failure(Error.unableToInitializeGraphRequest))
          return
       }

       _ = request.start { _, result, error in
          if let e = error {
             completion(.failure(e))
          } else if let result = result as? [String: Any], let data = result["data"] as? [String: Any] {
             if let isSilhouette = data["is_silhouette"] as? Bool, let urlString = data["url"] as? String {
                if isSilhouette {
                   completion(.success(nil))
                } else {
                   if let url = URL(string: urlString) {
                      completion(.success(url))
                   } else {
                      completion(.failure(Error.unexpectedGraphResponse))
                   }
                }
             } else {
                completion(.failure(Error.unexpectedGraphResponse))
             }
          } else {
             completion(.failure(Error.unexpectedGraphResponse))
          }
       }
    }
}


public enum Result<T> {

   case success(T)
   case failure(Swift.Error)

   public typealias Completion = (Result<T>) -> Void
}
Vlad
  • 6,402
  • 1
  • 60
  • 74