MENU

Typecho将评论头像修改为QQ头像

July 16, 2024 • Read: 104 • Typecho

默认的Gravatar实在是太难看了,尽管增加了国内的Gravatar源,还是偶尔会加载缓慢超时,干脆替换了。

默认代码:

<?php
}

private function outputAvatar($size = 32, $default = NULL) {
    if (Mirages::$options->commentsAvatar && !Mirages::$options->embedCommentOptions__disableQQAvatar && preg_match('/^(\d+)@qq.com$/i', $this->mail)) {
        if (NULL != Typecho_Router::get('mirages-api')) {
            $avatar = Typecho_Common::url(Typecho_Router::url('mirages-api', array("action" => "comment-avatar", "pathInfo" => $this->coid . "_" . $size . ".json")), Mirages::$options->index);
            echo '<img class="avatar" src=". STATIC_PATH ." images="true" spinner="true" svg="true" data-src=". $avatar ." data-type="json" alt="" this-="true">author . '" width="' . $size . '" height="' . $size . '" />';
            return;
        }
    }

    $this->gravatar($size, $default);
}

/**
 * 调用gravatar输出用户头像
 *
 * @access public
 * @param integer $size 头像尺寸
 * @param string $default 默认输出头像
 * @return void
 */
public function gravatar($size = 32, $default = NULL) {
    if ($this->options->commentsAvatar && 'comment' == $this->type) {
        $rating = $this->options->commentsAvatarRating;

        $this->pluginHandle(__CLASS__)->trigger($plugged)->gravatar($size, $rating, $default, $this);
        if (!$plugged) {
            $url = Typecho_Common::gravatarUrl($this->mail, $size, $rating, $default, true);
            echo '<img class="avatar" src=". STATIC_PATH ." images="true" spinner="true" svg="true" data-src=". $url ." alt="" this-="true">author . '" width="' . $size . '" height="' . $size . '" />';
        }
    }
}

情况一(全部输出自定义头像):

<?php
}private function outputAvatar($size = 32, $default = NULL) {
    if (Mirages::$options->commentsAvatar && !Mirages::$options->embedCommentOptions__disableQQAvatar && preg_match('/^(\d+)@qq.com$/i', $this->mail)) {
        if (NULL != Typecho_Router::get('mirages-api')) {
            $avatar = Typecho_Common::url(Typecho_Router::url('mirages-api', array("action" => "comment-avatar", "pathInfo" => $this->coid . "_" . $size . ".json")), Mirages::$options->index);
            echo '<img class="avatar" src="http://pic.seotool.vip/ai/222.jpg">';
            // echo '<img class="avatar" src=". STATIC_PATH ." images="true" spinner="true" svg="true" data-src=". $avatar ." data-type="json" alt="" this-="true">author . '" width="' . $size . '" height="' . $size . '" />';
            return;
        }
    }

    $this->gravatar($size, $default);
}

/**
 * 调用gravatar输出用户头像
 *
 * @access public
 * @param integer $size 头像尺寸
 * @param string $default 默认输出头像
 * @return void
 */
public function gravatar($size = 32, $default = NULL) {
    if ($this->options->commentsAvatar && 'comment' == $this->type) {
        $rating = $this->options->commentsAvatarRating;

        $this->pluginHandle(__CLASS__)->trigger($plugged)->gravatar($size, $rating, $default, $this);
        if (!$plugged) {
            $url = Typecho_Common::gravatarUrl($this->mail, $size, $rating, $default, true);
            echo '<img class="avatar" src="http://pic.seotool.vip/ai/222.jpg">';
            // echo '<img class="avatar" src=". STATIC_PATH ." images="true" spinner="true" svg="true" data-src=". $url ." alt="" this-="true">author . '" width="' . $size . '" height="' . $size . '" />';
        }
    }
}

情况二(获取评论者QQ头像,如获取不到或填写其他邮箱返回默认头像):

<?php
}private function outputAvatar($size = 32, $default = 'http://pic.seotool.vip/ai/222.jpg') {  
    if (Mirages::$options->commentsAvatar && !Mirages::$options->embedCommentOptions__disableQQAvatar) {  
        // 尝试从邮箱地址中提取QQ号  
        if (preg_match('/^(\d+)@qq\.com$/i', $this->mail, $matches)) {  
            $qqId = $matches[1]; // 提取的QQ号  

            // 尝试构建QQ头像的URL(注意:这可能会因为QQ的策略变化而失效)  
            $qqAvatarUrl = "http://q1.qlogo.cn/g?b=qq&nk={$qqId}&s={$size}&t=1600";  

            // 检查URL是否有效(实际上这里只是简单地构建URL,因为无法直接检查图片是否存在)  
            // 但为了代码的健壮性,我们可以尝试加载图片或设置一个备用方案  

            // 输出QQ头像的<img>标签  
            echo '<img class="avatar" src=". htmlspecialchars($qqAvatarUrl) ." alt="" htmlspecialchars="true" this-="true">author) . '" width="' . $size . '" height="' . $size . '" onerror="this.src=\'' . htmlspecialchars($default) . '\'"/>';  
            return;  
        }  
    }  

    // 如果无法获取QQ头像或邮箱不是QQ邮箱,则使用默认头像  
    echo '<img class="avatar" src=". htmlspecialchars($default) ." alt="" htmlspecialchars="true" this-="true">author) . '" width="' . $size . '" height="' . $size . '"/>';  
}  


Last Modified: August 4, 2024
Archives QR Code Tip
QR Code for this page
Tipping QR Code
Leave a Comment