Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP function (conversion of @jcsrb Javascript function at https://gist.github.com/jcsrb/1081548) to output URL for social icons from google, facebook, gravatar, twitter, tumblr and instagram)
<?
function get_avatar_from_service($service, $userid, $size) {
// Based on original Javascript function at https://gist.github.com/jcsrb/1081548
// 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 (via avatars.io) use get_avatar_from_service('twitter', username, default )
// for tumblr use get_avatar_from_service('tumblr', blog-url, size-in-px )
// for instagram (via avatars.io) use get_avatar_from_service('instagram', user-id, size-in-px )
// everything else will go to the fallback
// google and gravatar scale the avatar to any size, others will guided to the next best version
$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)
$sizeparam = '';
if (is_numeric($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":
// using avatars.io following twitter retirement of api v1, see http://avatars.io/
// retrieves default sized avatar, 48x48
$url = "http://avatars.io/twitter/" . $userid;
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
$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;
case "instagram":
// using avatars.io to get instagram avatar, see http://avatars.io/
// retrieves default sized avatar, 150x150
$url = "http://avatars.io/instagram/" . $userid;
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment