typecho替换Gavatar头像为QQ头像教程

因为国内Gavatar被墙,很多时候镜像源又不是那么好使还不如改成使用QQ头像来的稳妥,而且评论默认留邮箱使得更加完美。

步骤

  1. 将下面的代码写入 主题模板funtions.php 文件当中

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    //获取Gravatar头像 QQ邮箱取用qq头像
    function getGravatar($email, $s = 96, $d = 'mp', $r = 'g', $img = false, $atts = array())
    {
    preg_match_all('/((\d)*)@qq.com/', $email, $vai);
    if (empty($vai['1']['0'])) {
    $url = 'https://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 .= ' />';
    }
    }else{
    $url = 'https://q2.qlogo.cn/headimg_dl?dst_uin='.$vai['1']['0'].'&spec=100';
    }
    return $url;
    }
  2. 再将下面的代码复制到需要放头像的页面,通常是 comments.php

    1
    <?php $email=$comments->mail; $imgUrl = getGravatar($email);echo '<img src="'.$imgUrl.'" width="45px" height="45px" style="border-radius: 50%;" >'; ?>