现在入驻立享优惠!详情咨询客服>> QQ:115904045 QQ群:160457583 VIP群:540574329

您当前的位置:首页>>CSS教程>>PbootCMS教程

【腾讯云】爆款1核2G云服务器首年48元,还有iPad Pro、Bose耳机、京东卡等你来抽! 爆款云产品,新客特惠全年最低价,云服务器低至0.4折起

【网页图片加速】PbootCMS自动替换图片地址为七牛云cdn镜像链接

来源:超级管理员 发布时间:2021-11-28 热度:836 ℃

为了解决网站放在香港小水管主机图片加载太慢问题,或者放国内主机带宽小且网站图片多。此二开实现图片走七牛云等提供镜像存储的cdn服务商,这样可以明显提高网站加载速度。

加速原理:图片后台上传到本地,然后前台访问网页的时候图片会自动镜像一份到七牛云上,接着返回七牛云的图片链接到网页上显示,实现图片加速访问。

安心提示:此方法是本地依旧有图片,七牛云镜像一份,所以后期不用七牛云了,也不影响网站图片加载!

二开增加自动替换图片地址

1、\config\config.php里增加:(注意前一行需要以逗号结尾,默认官方版本无逗号需要自行加上)

// cdn链接地址,http(s)://img.xxx.com,尾巴不带“/”,单独调用{pboot:cdnurl}
'cdn_url' => '',

11.png

2、\apps\home\controller\ParserController.php里搜索function adjustLabelData,在其下方增加:

// 自动替换图片链接 @mk-cdn
if ($cdn_url = $this->config('cdn_url')) {
    if (strpos($data,$cdn_url)===false) {
        $src_ori_file = ROOT_PATH . $data;
        $out_cdn_file = rtrim($cdn_url,'/') . $data;
        if (! file_exists($out_cdn_file) && file_exists($src_ori_file) && $out_cdn_file!=rtrim($cdn_url,'/')) {
            $data = $out_cdn_file;
        }
    }
}

12.png

3、\apps\home\controller\ParserController.php里搜索{pboot:pageurl},在其下方增加:

$content = str_replace('{pboot:cdnurl}', rtrim($this->config('cdn_url'),'/'), $content); // 单独cdn地址调用标签 @mk-cdn

13.png

4、\apps\home\controller\IndexController.php里搜索$this->getContent($data),在其上方增加:

// 编辑器图片加cdn @mk-cdn
if ($cdn_url = Config::get('cdn_url')) {
    $data->content = str_replace('="/static/upload/', '="'.rtrim($cdn_url,'/').'/static/upload/', $data->content);
}

14.png

 至此,后面只要去config里添加cdn镜像链接即可。

七牛云镜像操作参考文章:http://www.dtmb.wang/cssjc/pbootcms/341.html

2021-07-26更新补充

经测试发现如果前台图片做了裁剪cdn替换图片地址会失效,因此还要做以下调整。

1、打开\apps\home\controller\ParserController.php

2、找到$maxheight = isset($params['maxheight']) ? $params['maxheight'] : null;下面增加一行

if ($cdn_url) $data = str_replace(rtrim($cdn_url,'/'), '', $data);

3、在这个判断if (! file_exists($max_out_file) && file_exists($max_src_file)) {结束的下一行增加

if ($cdn_url && file_exists($max_out_file)) {
    if (strpos($data,$cdn_url)===false) {
        $out_cdn_file_2 = rtrim($cdn_url,'/') . $data;
        if (! file_exists($out_cdn_file_2) && file_exists($max_out_file) && $out_cdn_file_2!=rtrim($cdn_url,'/')) {
            $data = $out_cdn_file_2;
        }
    }
}

15.png

4、找到$height = isset($params['height']) ? $params['height'] : null;下面增加一行

if ($cdn_url) $data = str_replace(rtrim($cdn_url,'/'), '', $data);

5、在这个判断if (! file_exists($out_file) && file_exists($src_file)) {结束的下一行增加

if ($cdn_url && file_exists($out_file)) {
    if (strpos($data,$cdn_url)===false) {
        $out_cdn_file_3 = rtrim($cdn_url,'/') . $data;
        if (! file_exists($out_cdn_file_3) && file_exists($out_file) && $out_cdn_file_3!=rtrim($cdn_url,'/')) {
            $data = $out_cdn_file_3;
        }
    }
}

16.png

2021-08-23 单页编辑器替换补充

打开apps\home\controller\IndexController.php,找到function getAbout后,

$content = $this->parser->parserAfter($content); // CMS公共标签后置解析

把上面代码下方增加:

// 编辑器图片加cdn @mk-cdn
if ($cdn_url = Config::get('cdn_url')) {
    $content = str_replace('="/static/upload/', '="'.rtrim($cdn_url,'/').'/static/upload/', $content);
}

效果截图:

17.png


发表评论:

评论记录:

未查询到任何数据!