很久很久以前就用过Gravatar这个网站的用户个性化头像服务,没有设置头像时默认是一个蓝底白字的G字,多数用户没设置的话看起来非常不和谐,发现StackOverflow的用户头像很有意思,准备趁这次BLOG大翻新时添加这个功能,搜索了一下相关实现其实这个头像就是Gravatar提供的,当用户邮箱没有设置过个性头像时生成各种颜色样式的方块图案,这种图案的名字叫:Identicons。
/**
* Get either a Gravatar URL or complete image tag for a specified email address.
*
* @param string $email The email address
* @param int $s Size in pixels, defaults to 80px [ 1 - 512 ]
* @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
* @param bool $img True to return a complete IMG tag False for just the URL
* @param array $atts Optional, additional key/value attributes to include in the IMG tag
* @return String containing either just a URL or a complete image tag
* @source http://gravatar.com/site/implement/images/php/
*/
function getGravatar($email, $s = 80, $d = 'identicon', $r = 'g', $img = false, $atts = array())
{
$url = 'http://www.gravatar.com/avatar/';
$url .= md5(strtolower(trim($email)));
$url .= "?s=$s&d=$d&r=$r";
if ($img) {
$url = '<img src="' . $url . '"';
foreach ($atts as $key => $val)
$url .= ' ' . $key . '="' . $val . '"';
$url .= ' />';
}
return $url;
}
目前没有留言,等您坐沙发呢!