Skip to content

Gravatar头像缓存再次修正版

2010 四月 29
by 荒野无灯

以前写过一篇文章叫《缓存Gravatar 头像,博客再次提速》,当初是直接通过修改wp函数来实现的,如果升级了WP版本,每次都要修改,因此还是有一点不方便。
这次补充的主要是通过插件机制来实现显示缓存头像,而不是修改WP文件.另外,新增 qq头像显示功能。
说明:用此修正版的代码前先要确保已经启用了GRAVATAR CACHE 程序。具体方法可见 《缓存Gravatar 头像,博客再次提速》

修正版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
######## START ############头像缓存#########BY 荒野无灯###############
//2010-05-08更新
function cache_avatar($avatar, $id_or_email, $size)
{
$host=get_bloginfo('wpurl');

    $email = '';
    $url='';
    if ( is_numeric($id_or_email) ) {
        $id = (int) $id_or_email;
        $user = get_userdata($id);
        if ( $user )
            $email = $user->user_email;
    } elseif ( is_object($id_or_email) ) {
        if ( isset($id_or_email->comment_type) && '' != $id_or_email->comment_type && 'comment' != $id_or_email->comment_type )
            return false; // No avatar for pingbacks or trackbacks

        if ( !empty($id_or_email->user_id) ) {
            $id = (int) $id_or_email->user_id;
            $user = get_userdata($id);
            if ( $user)
                $email = $user->user_email;
        } elseif ( !empty($id_or_email->comment_author_email) )
        {
            $email = $id_or_email->comment_author_email;
            $url=$id_or_email->comment_author_url;
        }
    } else {
        $email = $id_or_email;
    }
   
   

//如果评论者的博客是Qzone ,就用TA的Q头像。    
if ( !empty($url) && preg_match('/^(http:\/\/)?[1-9][0-9]*[\.a-zA-Z\/]+$/i', $url) ||  preg_match('/^[1-9][0-9]*@qq\.com$/i', $email))
 {
     $qq_face_url = 'http://qun.qq.com/cgi/svr/face/getface?type=1&uin=%s';    
//QQ头像地址, %s表示QQ号
        $qq = preg_replace('|\D*|', '', $url);
        if(!$qq)
            $qq = preg_replace('|\D*|', '', $email);
        $avatar = preg_replace('/src=\'[^\']*\'/', 'src=\'' . str_replace('%s', $qq, $qq_face_url) . '\'',  $avatar);
  }
 else
 {
 //否则就用gravatar头像
 $out = "$host/wp-content/gravatar_cache/cache/avatar/";
 $out .= md5( strtolower( $email ) );
 $avatar = "<img  src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
   }
  return $avatar;
}

add_filter('get_avatar', 'cache_avatar', 50,3);
########  END  ############头像缓存###########BY 荒野无灯#############

最初版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
######## START ############头像缓存#########BY 荒野无灯###############
function cache_avatar($avatar, $comment, $size)
{
$host=get_bloginfo('wpurl');
$qq_face_url = 'http://qun.qq.com/cgi/svr/face/getface?type=1&amp;uin=%s';    
//QQ头像地址, %s表示QQ号
//如果评论者的博客是Qzone ,就用TA的Q头像。    
if ( isset($comment->comment_author_url) && preg_match('/^(http:\/\/)?[1-9][0-9]*[\.a-zA-Z\/]+$/i', $comment->comment_author_url) ||  preg_match('/^[1-9][0-9]*@qq\.com$/i', $comment->comment_author_email))
 {
        $qq = preg_replace('|\D*|', '', $comment->comment_author_url);
        if(!$qq)
            $qq = preg_replace('|\D*|', '', $comment->comment_author_email);
        $avatar = preg_replace('/src=\'[^\']*\'/', 'src=\'' . str_replace('%s', $qq, $qq_face_url) . '\'',  $avatar);
  }
 else
 {
 //否则就用gravatar头像
 $out = "$host/wp-content/gravatar_cache/cache/avatar/";
 $out .= md5( strtolower( $comment->comment_author_email ) );
 $avatar = "<img  src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
   }
  return $avatar;
}

add_filter('get_avatar', 'cache_avatar', 50,3);
########  END  ############头像缓存###########BY 荒野无灯#############

喜欢这篇文章吗?

请订阅本站 RSS feed填写您的邮件地址,订阅我们的精彩内容:,欢迎点击这里捐赠以支持荒野无灯转播到腾讯微博 转播到腾讯微博

作者:荒野无灯
出处:Hacklog【Hacklog】

声明: 本站遵循 署名-非商业性使用-相同方式共享 3.0 共享协议. 转载请注明转自Hacklog【荒野无灯weblog】

本文链接: http://ihacklog.com/?p=3735

29 Responses Post a comment
  1. huhaku permalink
    九月 11, 2011

    你好,我想问一下,怎样才能修改成只识别Qmail而不识别url中的qq号码?

    • 九月 11, 2011

      注释掉QQ相关即可,我现在用的:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      ######## START ############头像缓存#########BY 荒野无灯###############
      //2010-05-08更新
      function cache_avatar($avatar, $id_or_email, $size)
      {
      $host=get_bloginfo('wpurl');

          $email = '';
          $url='';
          if ( is_numeric($id_or_email) ) {
              $id = (int) $id_or_email;
              $user = get_userdata($id);
              if ( $user )
                  $email = $user->user_email;
          } elseif ( is_object($id_or_email) ) {
              if ( isset($id_or_email->comment_type) && '' != $id_or_email->comment_type && 'comment' != $id_or_email->comment_type )
                  return false; // No avatar for pingbacks or trackbacks

              if ( !empty($id_or_email->user_id) ) {
                  $id = (int) $id_or_email->user_id;
                  $user = get_userdata($id);
                  if ( $user)
                      $email = $user->user_email;
              } elseif ( !empty($id_or_email->comment_author_email) )
              {
                  $email = $id_or_email->comment_author_email;
                  $url=$id_or_email->comment_author_url;
              }
          } else {
              $email = $id_or_email;
          }
         
         

      //如果评论者的博客是Qzone ,就用TA的Q头像。  
      //preg_match('/^(http:\/\/)?[1-9][0-9]*[\.a-zA-Z\/]+$/i', $url)
      //http://user.qzone.qq.com/595529846
      //http://595529846.qzone.qq.com/
          //现已不能获取头像,注释掉之
      /*
      if (
      !empty($url) &&  (preg_match('/^(http:\/\/)?user\.qzone\.qq\.com\/([1-9][0-9]+)$/i', $url ,$matches)  || preg_match('/^(http:\/\/)?([1-9][0-9]+)\.qzone\.qq\.com\/?$/i', $url ,$matches))
      ||
       preg_match('/^[1-9][0-9]*@qq\.com$/i', $email)
       )
       {
           //$qq_face_url = 'http://face8.qun.qq.com/cgi/svr/face/getface?type=1&amp;uin=%s';    
           $qq_face_url = 'http://face1.qun.qq.com/cgi/svr/face/getface?cache=0&type=1&fid=0&uin=%s&vfwebqq=2a3080d85214e831f30458971fdfa864d52f635d8e0ecb50a4bfc7fc8411a3d089519c0e3752e105';
      //QQ头像地址, %s表示QQ号
              //$qq = preg_replace('|\D*|', '', $url);
           if( is_numeric($matches[2]) )
               $qq=$matches[2];
           else
               $qq = preg_replace('|\D*|', '', $email);
           $avatar = preg_replace('/src=\'[^\']*\'/', 'src=\'' . str_replace('%s', $qq, $qq_face_url) . '\'',  $avatar);
        }
       else
       {
      */
       //否则就用gravatar头像
       $out = "$host/wp-content/gravatar_cache/cache/avatar/";
       $out .= md5( strtolower( $email ) );
       $avatar = "<img  src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
      /*   } */
        return $avatar;
      }

      add_filter('get_avatar', 'cache_avatar', 50,3);
      ########  END  ############头像缓存###########BY 荒野无灯#############
  2. 八月 14, 2011

    :?: 为啥我的博客头像不更新呢~~~

  3. 五月 3, 2011

    可以不加载那个开头那个啥程序就缓存了不啊?

  4. 四月 16, 2011

    博主,这个缓存的头像可以修改大小吗?这个缓存出来的头像在我的博客很模糊,可以修改下的吗?除了这个遗憾其他都很好用的,网站加载快了很多

  5. 四月 16, 2011

    试试效果,谢谢!!!

  6. 二月 5, 2011

    蛮不错,这个插件比较小巧,而且适用……我用willin的方法css得改,反正是很麻烦 :neutral:

  7. 十月 19, 2010

    :grin:
    咱来测试QQ头像显示.
    刚刚我那里不行.

  8. 五月 23, 2010

    但是既不现实原头像 也不现实自定义头像 :shock:

  9. 五月 23, 2010

    填qq邮箱要是能显示自定义头像就好啦 :mrgreen:

  10. Cv1 permalink
    五月 19, 2010

    友情测试下哈

  11. 五月 15, 2010

    不是自定义的qq头像啊

  12. 五月 15, 2010

    测试

  13. 五月 7, 2010

    跟其他的QQ头像显示的方式不太一样哦,其他只要在网址处填入QQ号就可以了哦。 :smile:

    • 五月 7, 2010

      我觉得这个更实用,如果你填写的邮箱是QQ邮箱(带号码的),就显示QQ头像,否则,再看你填写的博客地址是不是QQ空间,如果是则同样显示QQ头像。 :mrgreen:

  14. 五月 7, 2010

    试下QQ头像,尝试修改玩玩。

  15. 四月 30, 2010

    荒野大哥,这段代码是插到主题的functions.php里吗?

  16. 四月 30, 2010

    添qq邮箱,测试

    • 四月 30, 2010

      童鞋,QQ邮箱只能用QQ号的,如果用非QQ号码的QQ邮箱是没办法识别的。 :!:

  17. 四月 30, 2010

    听课要靠前坐。

  18. 四月 29, 2010

    填其它邮箱地址,网址填QQ空间(测试)

  19. 四月 29, 2010

    填 QQ邮箱,网址不填QQ空间。(测试)

Trackbacks & Pingbacks

  1. Gravatar -QQ头像緩存(三) - 电脑网络 - cache - gravatar - QQ - Winy Sky

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <q cite=""> <strong>

 :wink:  :-|  :-x  :twisted:  :)  8-O  :(  :roll:  :-P  :oops:  :-o  :mrgreen:  :lol:  :idea:  :-D  :evil:  :cry:  8)  :arrow:  :-?  :?:  :!:

Note: You may use basic HTML in your comments. Your email address will not be published.

Subscribe to this comment feed via RSS