大猫の意淫网志
  • 30
  • Jul

什么是 Gravatar

Gravatar 是 Globally Recognized Avatars (地球人公认的阿凡达)
可以去 http://gravatar.com/ 关联你的 Email 和头像
这样子在任何启用 Gravatar 的博客留言,即可显示对应的头像
例如 i(a)ooxx.me 对应的是 bigCat

Gravatar 是如何构建滴

一个 Gravatar 是服务器动态返回的图片,下面是一坨 src 图片地址

http://www.gravatar.com/avatar/ddb61c44b09ee3e8cb2700249ec9eac7&r=X&s=80

我们来肢解一下

Gravatar 总是以如下URL开头

http://www.gravatar.com/avatar/

接下去是 E-mail 对应的 Hash 结果, 这 E-mail 地址在 hash 之前必须要小写,并且要木有空格

ddb61c44b09ee3e8cb2700249ec9eac7

然后是 rating 分级, 有[ G | PG | R | X ] 4档
(在网站设置头像的时候会让你分级,建议选G)
约X的头像越不和谐,用专业的英语就叫 hardcore sexual
网址里这个分级设置的是容许最极端的那档
例如网址设置容许G,那么只有G的头像会显示
容许PG 会显示 G 和 PG
容许X 会显示 G 和 PG 和 R 和 X

&r=X

最后是 size 图像尺寸 , 有效范围是1到512

&s=80

还有一坨可选的默认头像地址,就是说如果email找不到对应的头像了或者不在分级范围内,就用这张默认图
可选参数为
* '404' (返回 a 404)
* 'mm' (mysteryman)
* 'identicon' (unique, generated image)
* 'monsterid' (unique, generated image)
* 'wavatar' (unique, generated image)
都是些奇形怪状的丑图,建议自定义一张拉轰的(http://ooxx.me/me.jpg)

&d=http%3A%2F%2Fooxx.me%2Fme.jpg

那么在主题里调用 Gravatar 就很简单的用

 <?php
   echo get_avatar(i@ooxx.me,50);
   ?>

这里的邮件地址可以用 get_the_author_meta('user_email') 获取本文作者的email
如果用在评论循环里,可以用 get_comment_author_email() 获取当前循环里的email
记得小写哦 strtolower(get_comment_author_email()) 会自动转为小写
记得MD5哦 md5(strtolower(get_comment_author_email())) 会进行 hash

加起来就是

 <?php
   echo get_avatar(md5(strtolower(get_comment_author_email())),50);
   ?>

会吐出来

<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />

不幸的是get_avatar()默认吐img标签
幸运的是如果只要src值,可以这么写

<?php
echo "http://www.gravatar.com/avatar/".md5(strtolower(get_comment_author_email()))."&r=X&s=80";
?>

挨吻老湿就是这么写再结合jQuery 配合 CSS3 搞图片圆角来输出头像滴
点这里看

假装异步加载ing

» You can leave a comment, or trackback from your own site.

大战 44 回合 to “如何获取 Gravatars 头像的路径”

  1. whatUwant says:

    能举报楼上的吗。。。 :lol:

    Reply

  2. trylife says:

    :lol: :P :x :evil: :twisted: 这些表情都是啥意思~~

    Reply

  3. 卡巴飞机 says:

    :idea: :twisted: 叫兽

    Reply

  4. finvola says:

    @祛痘产品排行榜, 哟传说中的垃圾留言拦截器失效了哟

    Reply

    bigCat:

    @finvola, 已经添加到spam服务器了,他在其他blog也会被菊爆的

    Reply

  5. MOPVHS says:

    :o 大猫老师V5~~~~、

    用头像缓存的飘过...

    Reply

    MOPVHS:

    @MOPVHS, 明明想打“老湿”的~~~搜狗不配合呀~~ :evil:

    Reply

  6. hzlzh says:

    现在就缺动态的Avatar,我的twitter都能都能动了,应该是gif不好resize导致的

    Reply

    bigCat:

    @hzlzh, gif的体积也是个问题... 我可不想被拖慢速度...

    Reply

    hzlzh:

    @bigCat, 那就缓存之,神一般的加载速度

    Reply

    bigCat:

    @hzlzh, 呃,我担心的不是dns查询时间,也不是http 请求数 是体积,一般GIF都比较大
    服务器端缓存解决的问题就是DNS查询问题而已,因为主机都在国外,搞不好还没gravatar的线路速度快

    Reply

  7. mice says:

    我勒个去...沙发什么都没了..

    Reply

  8. A.shun says:

    把Gravatars缓存到自己服务器的路过 ;)

    这的圆角样式真好看 :!:

    Reply

  9. meters says:

    从这儿过去,发现ivane好久都没更新过日志了...
    学习啦... :lol:

    Reply

  10. trylife says:

    我勒个去表情变了

    Reply

    bigCat:

    @trylife, after a upgrade... I'll fix it later

    Reply

    trylife:

    @bigCat, i know ,now~

    Reply

  11. [...] This post was mentioned on Twitter by Carrie, 晓墨墨. 晓墨墨 said: 如何获取 Gravatars 头像的路径... http://is.gd/dUwKq [...]

  12. belts says:

    飘来飘去的,看看

    Reply

  13. sunglasses says:

    弄个变形金刚,哈哈

    Reply

  14. Kimcool says:

    没有实际意义,意淫下。

    Reply

  15. chisdy says:

    原来还有这么多设置的,外行看热闹了

    Reply

  16. max says:

    弄个哈里波特,呵呵

    Reply

  17. WordPress啦 says:

    呵呵,这些咱只知道用,

    Reply

  18. zhy says:

    好文章要收藏 :lol:

    Reply

  19. meters says:

    你还不变灰!!!!

    Reply

    bigCat:

    @meters, 操,再和我提灰这个字,我爆你菊花

    Reply

  20. ChaoLen says:

    额,一般都是登录进去复制链接!囧~

    Reply

  21. very says:

    呵呵,很好很强大

    Reply

  22. [...] 以上内容引用自大猫老师:http://ooxx.me/get_avatar_src.orz WordPress官网对Gravatar的介绍:Using Gravatars [...]

  23. imfigo says:

    头像有就行 :( 。懒得折腾了。

    Reply

  24. 阅网博客 says:

    “一坨 src 图片地址”、“肢解”、“木有空格”。。。。大猫兄弟的博文读起来总是很动情、很怡美、很吸引人。

    Reply

    bigCat:

    @阅网博客, 排比句表强调,嘿嘿

    Reply

  25. laowi says:

    :oops: :oops: 这套表情挺好玩的哦

    Reply

    bigCat:

    @laowi, zing老湿画滴

    Reply

  26. airmax90 says:

    太抽象了,俺们看不懂 :oops:

    Reply

    bigCat:

    @airmax90, 感受就对鸟

    Reply

  27. wmtimes says:

    我也来试试。不过我的怎么觉得不动画了呢。

    Reply

    bigCat:

    @wmtimes, 虾米?

    Reply

  28. 手机短信 says:

    终于找到了~感谢博主

    Reply

Leave a Reply

:!: :roll: :o :lol: :P :( 8O :x :oops: :) :?: :idea: :evil: ;) :twisted: XHTML<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>