JOE主题(7.7.1)直接改,其他主题通用,替换主题里关于获取头像的代码即可;
打开 Joe/core/function.php 文件找到以下代码(171~183);
/* 通过邮箱生成头像地址 */ function _getAvatarByMail($mail) { $gravatarsUrl = Helper::options()->JCustomAvatarSource ? Helper::options()->JCustomAvatarSource : 'https://gravatar.helingqi.com/wavatar/'; $mailLower = strtolower($mail); $md5MailLower = md5($mailLower); $qqMail = str_replace('@qq.com', '', $mailLower); if (strstr($mailLower, "qq.com") && is_numeric($qqMail) && strlen($qqMail) < 11 && strlen($qqMail) > 4) { echo 'https://thirdqq.qlogo.cn/g?b=qq&nk=' . $qqMail . '&s=100'; } else { echo $gravatarsUrl . $md5MailLower . '?d=mm'; } };替换成如下代码即可;
{hide}/* 通过邮箱生成头像地址 */ function _getAvatarByMail($mail) { $gravatarsUrl = Helper::options()->JCustomAvatarSource ? Helper::options()->JCustomAvatarSource : 'https://gravatar.helingqi.com/wavatar/'; $mailLower = strtolower($mail); $md5MailLower = md5($mailLower); if (strstr($mailLower, "qq.com")) { echo 'https://cn.cravatar.com/avatar/' . $md5MailLower . '&s=100'; } else { echo $gravatarsUrl . $md5MailLower . '?d=mm'; } };{/hide}
Jonty Blog修改版JOE2,其他主题通用
使用JOE2主题的用户打开 Joe/library/avatar.php 替换成如下代码;
{hide}<?php /** * 优化版头像代理脚本 * https://nobb.cc/usr/themes/Joe/library/avatar.php?data=dzQ5QHFxLmNvbQ==&f=1 * 支持: * ?data=MD5 * ?data=QQ号 * ?data=QQ邮箱 * ?data=Base64(纯QQ号) * ?data=Base64(QQ邮箱) */ error_reporting(0); ob_clean(); // --- 1. 配置区 --- //$customSource = 'https://dn-qiniu-avatar.qbox.me/avatar/'; $customSource = 'https://cn.cravatar.com/avatar/'; $expireTime = 604800; // 7天 $cacheDir = __DIR__ . '/../assets/cache/avatars'; $defaultDir = $cacheDir . '/default'; // --- 2. 参数处理 --- $data = $_GET['data'] ?? ''; $forceUpdate = isset($_GET['f']) && $_GET['f'] == '1'; if (empty($data)) { header("HTTP/1.1 404 Not Found"); exit; } $mail = ''; $md5Mail = ''; $isQQ = false; /** * 3. 核心:输入类型识别与转换 */ // 情况 A:直接传入 MD5 (32位) if (preg_match('/^[a-f0-9]{32}$/i', $data)) { $md5Mail = $data; goto PROCESS; } // 情况 B:直接传入纯 QQ 号 if (preg_match('/^\d{5,11}$/', $data)) { $mail = $data . '@qq.com'; $isQQ = true; } // 情况 C:直接传入 QQ 邮箱 elseif (preg_match('/^\d+@qq\.com$/i', $data)) { $mail = $data; $isQQ = true; } // 情况 D:Base64 编码 else { $decoded = base64_decode($data, true); if ($decoded === false) { http_response_code(404); exit; } // 关键逻辑:判断 Base64 解码后是否为纯 QQ 号 if (preg_match('/^\d{5,11}$/', $decoded)) { $mail = $decoded . '@qq.com'; $isQQ = true; } else { $mail = $decoded; } } $mailLower = strtolower(trim($mail)); $md5Mail = md5($mailLower); PROCESS: // --- 4. 缓存逻辑 --- if (!is_dir($cacheDir)) @mkdir($cacheDir, 0777, true); if (!is_dir($defaultDir)) @mkdir($defaultDir, 0777, true); $realFilePath = $cacheDir . '/' . $md5Mail . '.jpg'; $placeholder = $defaultDir . '/' . $md5Mail . '.jpg'; if ($forceUpdate) { @unlink($realFilePath); @unlink($placeholder); } if (file_exists($realFilePath) && (time() - filemtime($realFilePath) < $expireTime)) { serveImage($realFilePath); } if (file_exists($placeholder) && (time() - filemtime($placeholder) < $expireTime)) { serveDefault(__DIR__ . '/default.jpg'); } // --- 5. 下载逻辑 --- if ($isQQ) { $qq = str_replace('@qq.com', '', $mailLower); $avatarSource = "https://thirdqq.qlogo.cn/g?b=qq&nk={$qq}&s=100"; } else { $avatarSource = rtrim($customSource, '/') . '/' . $md5Mail . '?s=100&d=404'; } $context = stream_context_create([ 'http' => ['timeout' => 5, 'header' => "User-Agent: Mozilla/5.0"], 'ssl' => ['verify_peer' => false] ]); $imgData = @file_get_contents($avatarSource, false, $context); $httpStatus = $http_response_header[0] ?? ''; // --- 6. 存储与输出 --- if ($imgData && strpos($httpStatus, '200') !== false) { file_put_contents($realFilePath, $imgData); @unlink($placeholder); serveImage($realFilePath); } else { file_put_contents($placeholder, ''); serveDefault(__DIR__ . '/default.jpg'); } /** * 输出图片并支持浏览器缓存 */ function serveImage($file) { $etag = md5_file($file); header("Etag: $etag"); if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) { header('HTTP/1.1 304 Not Modified'); exit; } header('Content-Type: image/jpeg'); header('Cache-Control: public, max-age=604800'); header('Content-Length: ' . filesize($file)); readfile($file); exit; } function serveDefault($defaultImg) { if (file_exists($defaultImg)) { serveImage($defaultImg); } else { header("HTTP/1.1 404 Not Found"); exit; } }{/hide}
打开 Joe/core/function.php 找到 $encodedMail = base64_encode($mail); (320行),把 $encodedMail = 后面的变量修改为自己想要的就行了
前端 ?data=显示 $encodedMail =写法 评论头像URL MD5 md5(strtolower(trim($mail))) avatar.php?data=09359EEC22D4373CD2E7C3AEB703A8FC QQ/QQ邮箱 $encodedMail = $mail avatar.php?data=XXXXXQQ.COM Base64 base64_encode($mail) avatar.php?data=ODg4ODg4ODg= 示例:$encodedMail = md5(strtolower(trim($mail)));
头像:/Joe/library/avatar.php?data=09359EEC22D4373CD2E7C3AEB703A8FC
说明:data= 可直接填写QQ、任何邮箱、QQ和邮箱的Base64码、MD5 调用- 其他主题使用同款接口时,新建一个.php文件写入上面的PHP代码;
找到自己使用主题获取头像代码(通常在 function.php ),替换成如下代码;
{hide}/* 通过邮箱生成头像地址-文件缓存版 */ function _getAvatarByMail($mail) { $themeUrl = Helper::options()->themeUrl; $encodedMail = md5(strtolower(trim($mail))); //根据自己的喜欢选择 $proxyUrl = $themeUrl . '/library/avatar.php?data=' . $encodedMail; echo $proxyUrl; }{/hide}
打开 comment.php 把头像地址(src)中的函数修改如下,如果使用主题作者用一样的函数则不需要修改;
{hide}<?php _getAvatarByMail($comments->mail); ?>{/hide}
谢谢