9

How do I check whether user's profile picture is default or uploaded in Google?

Note: When you get user details from APIs.

Google User's Default Image

Love Sharma
  • 1,981
  • 1
  • 18
  • 37

7 Answers7

9

All of default profile pictures have the following URL:

https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg

You can just compare string equality of profile picture with the given one.

teymourlouie
  • 6,645
  • 2
  • 21
  • 13
  • 1
    this string seem to have changed but it's still true that default images contain `AAAAAAAAAAI/AAAAAAAAAAA` and ends in `photo.jpg`. – Dmitry Minkovsky Mar 26 '20 at 19:16
  • This has changed and default images don't contain such URL patterns anymore. To know whether the user profile picture is real or default, we can leverage the ``people`` API as mentioned by @incaren in the below answer. – Adnan Mohib Jan 12 '23 at 07:32
7

people.get includes a isDefault value in the image object. E.g. if you try it for +Google you will get;

"image": {
    "url": "https://lh4.googleusercontent.com/-v0soe-ievYE/AAAAAAAAAAI/AAAAAAACyas/yR1_yhwBcBA/photo.jpg?sz=50",
    "isDefault": false
}
abraham
  • 46,583
  • 10
  • 100
  • 152
7

people.get no longer has isDefault as a property. https://developers.google.com/+/web/api/rest/latest/people#resource

Nat
  • 300
  • 3
  • 8
3

Updating this answer for 2020: it's now possible to get the user's profile picture by sending a request to the people.get API with photos as the personFields to request.

You'll get back an array of images, and whenever default: true is present, it means it's a default (not user-set) image:

Example (if you're using this with OAuth):

GET https://people.googleapis.com/v1/people/me

Sample response (with profile picture)

{
  "resourceName": "people/117055959617985617271",
  "etag": "%EgQBAzcuGgQBAgUHIgxHamp6MG9wZ3hOcz0=",
  "photos": [
    {
      "metadata": {
        "primary": true,
        "source": {
          "type": "PROFILE",
          "id": "117055959617985617271"
        }
      },
      "url": "https://lh3.googleusercontent.com/a-/AOh14Gg_-udXd3O6K1URTBTEK2rLa2DOh6G2dgmOHcOBOtE=s100"
    },
    {
      "metadata": {
        "source": {
          "type": "CONTACT",
          "id": "2"
        }
      },
      "url": "https://lh3.googleusercontent.com/a/default-user=s100",
      "default": true
    }
  ]
}

Sample response (default only)

{
  "resourceName": "people/113009277022343767972",
  "etag": "%EgQBAzcuGgQBAgUHIgxxdHVTY3IxZVJIUT0=",
  "photos": [
    {
      "metadata": {
        "primary": true,
        "source": {
          "type": "PROFILE",
          "id": "113009277022343767972"
        }
      },
      "url": "https://lh6.googleusercontent.com/-6NS3awoYRXw/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnTo-mElIpcAEazDV9DAs1VyYDEIw/s100/photo.jpg",
      "default": true
    }
  ]
}
incaren
  • 191
  • 1
  • 6
0

in ruby with devise and omniauth-google-oauth2

in your devise.rb

config.omniauth :google_oauth2 (Other options....), skip_image_info: false

then in your user.rb / other place:

def self.parse_auth_image(auth)
  if auth.provider == "google_oauth2"
    return nil if auth.dig("extra", "raw_info", "image", "isDefault")
  end
  auth.info.image
end
pedro
  • 41
  • 5
-1

The best way to do this in FULL DETAIL:

require 'open-uri'

Default image:

default = "https://lh3.googleusercontent.com/-
XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg"

Image to check:

image_to_check = "https://lh3.googleusercontent.com/-
uh4wK1JDtCI/AAAAAAAAAAI/AAAAAAAAAAA/huieBSbV4zg/s64-
c/100695019739939096169.jpg"

Comparison check

blob_for_default_image = open(default).read

blob_for_image_to_check = open(image_to_check).read 

Then you compare the 2 image blobs:

blob_for_default_image == blob_for_image_to_check 
-2

If you are using PHP, It's fairly simple, just use this code

$profile_img_end = explode("/", $userData['picture']); // Exploding the URL and Dividing it into several terms

if (end($profile_img_end) === "photo.jpg") { // Now Simply Check if last part of array is equal to "photo.jpg" which is common in all default images
    $profile_img = null; // If it's default then set it equal to null
} else {
    $profile_img = $userData['picture']; // Else get the Link of the Image
}

Alternative in JavaScript

var url = ""; // Image URL

var img_split = url.split("/"); // Split it Again from / (forward slash)

if (img_split[img_split.length - 1] === 'photo.jpg') { // Check if last array is equal to photo.jpg (By Default Image)
   var image = null;
} else {
   var image = url;
}

HOPE IT HELPS!

Z Coder
  • 1
  • 1