Discuz! X 手机版图片链接静态化+缓存的修改方法-Discuz教程下载

Discuz! X 手机版图片链接静态化+缓存的修改方法

开通本站Svip会员,全站资源免费下
注:个人方案,不建议直接采用,仅供参考。
默认情况下,手机版帖内图片会进行裁切,链接为 forum.php?mod=image&aid=... ,可以通过以下修改直接载入CDN的静态链接。

1、应用前提:
创建 /extra/cdn 目录,目录内增加软链接 shequ 指向上传目录。
对该目录做一个独立的反向代理站点(反代到 源站/extra/cdn/),
后台“上传设置 - 基本设置 - 本地附件 URL 地址”处设置为该站点的地址。

源站用以下代码设置404页面:
  1. error_page  404 = /404.php;
复制代码
注意必须添加其中的 = 号,否则无法返回其中指定的状态,无法正常工作。

404.php的内容为:
  1. <?php
  2. if(strpos($_SERVER["REQUEST_URI"], "/extra/cdn/shequ/image/") !== false) {
  3.         $thumbpath = str_replace("/extra/cdn/shequ/image/", "", $_SERVER["REQUEST_URI"]);
  4.         $thumbpath_removeExtension = explode(".", $thumbpath);
  5.         $idwh = explode("_", $thumbpath_removeExtension[0]);
  6.         $aid = intval(str_replace("/", "", $idwh[0]));
  7.         $w = $idwh[1]; $h = $idwh[2];
  8.         $key = substr(md5($aid.'|'.$w.'|'.$h.'你的authkey'), 0, 16); //字符串须与 config/config_global.php 里的 authkey 保持一致
  9.         if(!$aid || !$w || !$h || !$key) exit('Error: Missing parameters');
  10.         $getURL = get('https://源站域名/forum.php?mod=image&aid='.$aid.'&size='.$w.'x'.$h.'&key='.rawurlencode($key).'&type=fixnone'); //图片不存在时请求此链接生成
  11.         header("Location: $getURL");
  12. } else {
  13.         header("HTTP/1.1 410 Gone");
  14.         include "404.html";
  15. }

  16. function get($url) {
  17.         $request = curl_init();
  18.         curl_setopt($request, CURLOPT_HTTPGET, true);
  19.         curl_setopt($request, CURLOPT_URL, $url);
  20.         $return_url = curl_getinfo($request, CURLINFO_EFFECTIVE_URL);
  21.         $response = curl_exec($request);
  22.         curl_close($request);
  23.         return $return_url;
  24. }

  25. ?>
复制代码
目的:当反代获取上传目录下image文件夹(就是裁切图文件夹)中的图片时,如果图片不存在,自动请求站点生成裁切图,然后返回静态图片地址,让客户端浏览器重新加载。此时自建的反向代理站点应把这张图片缓存起来,这样下次用户请求这张图片时,就不需要系统再生成了。

2、打开 source/function/function_core.php ,
查找:
  1. function getforumimg($aid, $nocache = 0, $w = 140, $h = 140, $type = '') {
  2.         global $_G;
  3.         $key = dsign($aid.'|'.$w.'|'.$h);
  4.         return 'forum.php?mod=image&aid='.$aid.'&size='.$w.'x'.$h.'&key='.rawurlencode($key).($nocache ? '&nocache=yes' : '').($type ? '&type='.$type : '');
  5. }
复制代码
替换为:
  1. function getforumimg($aid, $nocache = 0, $w = 140, $h = 140, $type = '') {
  2.         global $_G;
  3.         $key = dsign($aid.'|'.$w.'|'.$h);
  4.         return helper_attach::attachpreurl().'image/'.helper_attach::makethumbpath($aid, $w, $h);
  5. }
复制代码

其中的 helper_attach::attachpreurl() 表示后台“上传设置 - 基本设置 - 本地附件 URL 地址”处设置的地址。

全部评论 0

您需要登录后才可以回帖 立即登录
登录
0
0
0
返回顶部