How to check whether Google User's image is default or uploaded?
Asked Answered
D

7

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

Dorree answered 25/12, 2014 at 18:25 Comment(0)
S
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.

Slemmer answered 8/5, 2017 at 9:1 Comment(2)
this string seem to have changed but it's still true that default images contain AAAAAAAAAAI/AAAAAAAAAAA and ends in photo.jpg.Mitman
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.Finnie
M
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
}
Maisiemaison answered 25/12, 2014 at 20:35 Comment(2)
What is it in case it's not available? False by default, or true by default?Tamper
@androiddeveloper It's false by default: if default is omitted from the API, it means that it's user-setAscidian
S
7

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

Seta answered 21/8, 2015 at 17:57 Comment(1)
I tried this a minute ago. It gave isDefault": false. Please can you clarify this.Vittle
A
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
    }
  ]
}
Ascidian answered 17/6, 2020 at 20:37 Comment(0)
S
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
Sportswear answered 21/12, 2017 at 19:5 Comment(0)
P
-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 
Polymerization answered 1/3, 2018 at 19:17 Comment(0)
K
-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!

Kudva answered 18/3, 2021 at 16:31 Comment(1)
This answer can not be true...not for 2021(?) At least in 2023 the image url looks like that:Tetanus

© 2022 - 2024 — McMap. All rights reserved.