Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
get avatar from google profiles, facebook, gravatar, twitter, tumblr
function get_avatar_from_service(service, userid, size) {
// this return the url that redirects to the according user image/avatar/profile picture
// implemented services: google profiles, facebook, gravatar, twitter, tumblr, default fallback
// for google use get_avatar_from_service('google', profile-name or user-id , size-in-px )
// for facebook use get_avatar_from_service('facebook', vanity url or user-id , size-in-px or size-as-word )
// for gravatar use get_avatar_from_service('gravatar', md5 hash email@adress, size-in-px )
// for twitter use get_avatar_from_service('twitter', username, size-in-px or size-as-word )
// for tumblr use get_avatar_from_service('tumblr', blog-url, size-in-px )
// everything else will go to the fallback
// google and gravatar scale the avatar to any site, others will guided to the next best version
var url = '';
switch (service) {
case "google":
// see http://googlesystem.blogspot.com/2011/03/unedited-google-profile-pictures.html (couldn't find a better link)
// available sizes: all, google rescales for you
url = "http://profiles.google.com/s2/photos/profile/" + userid + "?sz=" + size;
break;
case "facebook":
// see https://developers.facebook.com/docs/reference/api/
// available sizes: square (50x50), small (50xH) , normal (100xH), large (200xH)
var sizeparam = '';
if (isNumber(size)) {
if (size >= 200) {
sizeparam = 'large'
};
if (size >= 100 && size < 200) {
sizeparam = 'normal'
};
if (size >= 50 && size < 100) {
sizeparam = 'small'
};
if (size < 50) {
sizeparam = 'square'
};
} else {
sizeparam = size;
}
url = "https://graph.facebook.com/" + userid + "/picture?type=" + sizeparam;
break;
case "gravatar":
// see http://en.gravatar.com/site/implement/images/
// available sizes: all, gravatar rescales for you
url = "http://www.gravatar.com/avatar/" + userid + "?s=" + size
break;
case "twitter":
// see https://dev.twitter.com/docs/api/1/get/users/profile_image/%3Ascreen_name
// available sizes: bigger (73x73), normal (48x48), mini (24x24), no param will give you full size
var sizeparam = '';
if (isNumber(size)) {
if (size >= 73) {
sizeparam = 'bigger'
};
if (size >= 48 && size < 73) {
sizeparam = 'normal'
};
if (size < 48) {
sizeparam = 'mini'
};
} else {
sizeparam = size;
}
url = "http://api.twitter.com/1/users/profile_image?screen_name=" + userid + "&size=" + sizeparam;
break;
case "tumblr":
// see http://www.tumblr.com/docs/en/api/v2#blog-avatar
//TODO do something smarter with the ranges
// available sizes: 16, 24, 30, 40, 48, 64, 96, 128, 512
var sizeparam = '';
if (size >= 512) {
sizeparam = 512
};
if (size >= 128 && size < 512) {
sizeparam = 128
};
if (size >= 96 && size < 128) {
sizeparam = 96
};
if (size >= 64 && size < 96) {
sizeparam = 64
};
if (size >= 48 && size < 64) {
sizeparam = 48
};
if (size >= 40 && size < 48) {
sizeparam = 40
};
if (size >= 30 && size < 40) {
sizeparam = 30
};
if (size >= 24 && size < 30) {
sizeparam = 24
};
if (size < 24) {
sizeparam = 16
};
url = "http://api.tumblr.com/v2/blog/" + userid + "/avatar/" + sizeparam;
break;
default:
// http://www.iconfinder.com/icondetails/23741/128/avatar_devil_evil_green_monster_vampire_icon
// find your own
url = "http://i.imgur.com/RLiDK.png"; // 48x48
}
return url;
}
// helper methods
function isNumber(n) {
// see http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric
return !isNaN(parseFloat(n)) && isFinite(n);
}
@JoeHetfield

This comment has been minimized.

Copy link

JoeHetfield commented Oct 16, 2013

Thanks, very helpful

@Dottenpixel

This comment has been minimized.

Copy link

Dottenpixel commented Oct 24, 2013

What form would the userid argument take for a google account that's not gmail? The google seems to only work with gmail user IDs. For example, with joesmith@gmail.com, I can pass "joesmith" as the userid argument and it works.

@makeshyft-tom-g

This comment has been minimized.

Copy link

makeshyft-tom-g commented Mar 20, 2014

Thank you for this!

@neotropic2023

This comment has been minimized.

Copy link

neotropic2023 commented Apr 3, 2014

@neotropic2023

This comment has been minimized.

Copy link

neotropic2023 commented Apr 3, 2014

Google, no API needed:
$data = file_get_contents('http://picasaweb.google.com/data/entry/api/user/108000104043020660261?alt=json');
$d = json_decode($data);
$avatar = $d->{'entry'}->{'gphoto$thumbnail'}->{'$t'};
// Outputs example: https://lh3.googleusercontent.com/-2N6fRg5OFbM/AAAAAAAAAAI/AAAAAAAAADE/2-RmpExH6iU/s64-c/photo.jpg

CHANGE: the 64 in "s64" for the size

@neotropic2023

This comment has been minimized.

Copy link

neotropic2023 commented Apr 3, 2014

Sorry, for some reason I thought this was PHP, lol. Silly me.

@Braunson

This comment has been minimized.

Copy link

Braunson commented Apr 21, 2014

@neotropic2023 thanks for the Google update!

@DevinWalker

This comment has been minimized.

Copy link

DevinWalker commented May 29, 2014

@neotropic2023 - Not sure how you figured out that bit but it's some good code!

@DevinWalker

This comment has been minimized.

Copy link

DevinWalker commented May 29, 2014

@neotropic2023 - is there anyway you know of to get a business avatar using picasaweb? The code only seems to pull user profile pictures, not businesses as well.

@mvujica

This comment has been minimized.

Copy link

mvujica commented Jul 15, 2015

@gibatronic

This comment has been minimized.

Copy link

gibatronic commented Aug 3, 2015

All Twitter solutions posted here don't work anymore.
You have to use the new authenticated API:
dev.twitter.com/rest/reference/get/users/show

@ibruno

This comment has been minimized.

Copy link

ibruno commented Sep 4, 2015

@YanaSavchenko

This comment has been minimized.

Copy link

YanaSavchenko commented Oct 30, 2015

Facebook's 'large' type is kinda small (200x200). You can use another trick!
https://graph.facebook.com/{user.id}/picture?width=400&height=400

It will cut image to some closer dimension (for 400px - 480px, for 1000px - 689px and so on).

And for github's images you can add param s.
{avatar_url}&s=48
But it doens't make your image too big. I think it's depends on source image size

I wish this info will be helpful!

Have fun

@infiniteloopltd

This comment has been minimized.

Copy link

infiniteloopltd commented Jul 31, 2016

There's an API for getting the Google profile pic and name from an email address without having to sign in -
it's at http://www.avatarapi.com/ - The docs have code in C#, PHP and vanilla HTTP

@SociallyDev

This comment has been minimized.

Copy link

SociallyDev commented Jun 19, 2017

I created a completely free API service, Dev Identify, it fetches data from sites like Google, G+ and Gravatar just by giving the email of the person! It provides name, location and profile picture of the person. https://devidentify.com

@karldd

This comment has been minimized.

Copy link

karldd commented Sep 20, 2017

@SociallyDev
Looks good, but could you enable CORS?
The responses don't seem to have the Access-Control-Allow-Origin header:

$ curl -H "Origin: http://localhost:8080" --verbose https://api.devidentify.com/sjobs@apple.com
*   Trying 104.18.44.46...
* Connected to api.devidentify.com (104.18.44.46) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
* Server certificate: sni24922.cloudflaressl.com
* Server certificate: COMODO ECC Domain Validation Secure Server CA 2
* Server certificate: COMODO ECC Certification Authority
* Server certificate: AddTrust External CA Root
> GET /sjobs@apple.com HTTP/1.1
> Host: api.devidentify.com
> User-Agent: curl/7.43.0
> Accept: */*
> Origin: http://localhost:8080
@cheonhau

This comment has been minimized.

Copy link

cheonhau commented Jan 2, 2018

how do i find id of user equal email ? (facebook)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.