问题:typecho个人头像服务器为gravatar,经常会被国内墙。
解决思路:1.走国内cdn代理源;2.直接部署到本地。
经过考虑后,我选择第二种方案。
解决方法:
以头像源文件地址test.demo.com/icon.jpg为例。
1.在网站源目录/var/Typecho/Common.php文件第826-839行之间的代码:
public static function gravatarUrl(
?string $mail,
int $size,
?string $rating = null,
?string $default = null,
bool $isSecure = true
): string {
if (defined('__TYPECHO_GRAVATAR_PREFIX__')) {
$url = __TYPECHO_GRAVATAR_PREFIX__;
} else {
$url = $isSecure ? 'https://secure.gravatar.com' : 'http://www.gravatar.com';
$url .= '/avatar/';
}
修改成:
public static function gravatarUrl(
int $size,
bool $isSecure = true
): string {
if (defined('__TYPECHO_GRAVATAR_PREFIX__')) {
$url = __TYPECHO_GRAVATAR_PREFIX__;
} else {
$url = $isSecure ? 'https://test.demo.com/icon.jpg' : 'http://test.demo.com/icon.jpg';
}
2.在网站源目录/var/Widget/Base/Users.php中第212-217行:
public function gravatar(int $size = 40, string $rating = 'X', ?string $default = null, ?string $class = null)
{
$url = Common::gravatarUrl($this->mail, $size, $rating, $default, $this->request->isSecure());
echo '<img' . (empty($class) ? '' : ' class="' . $class . '"') . ' src="' . $url . '" alt="' .
$this->screenName . '" width="' . $size . '" height="' . $size . '" />';
}
修改成:
public function gravatar(int $size = 40, string $rating = 'X', ?string $default = null, ?string $class = null)
{
$url = Common::gravatarUrl($size, $this->request->isSecure());
echo '<img' . (empty($class) ? '' : ' class="' . $class . '"') . ' src="' . $url . '" alt="' .
$this->screenName . '" width="' . $size . '" height="' . $size . '" />';
}
3.在网站源目录/admin/profile.php中第14-16行:
<p><a href="https://gravatar.com/emails/"
title="<?php _e('在 Gravatar 上修改头像'); ?>"><?php echo '<img class="profile-avatar" src="' . \Typecho\Common::gravatarUrl($user->mail, 220, 'X', 'mm', $request->isSecure()) . '" alt="' . $user->screenName . '" />'; ?></a>
</p>
删掉。
即可实现gravatar本地部署。
优点:不用挂梯子特地上一次gravatar官网;
缺点:
1.后续修改头像时要直接替换源文件,比较麻烦;
2.个人设置中的gravatar设置会被干掉:
最后发条文章测试一下: