我的知识记录

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

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

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

2、appshomecontrollerParserController.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;
        }
    }
}

3、appshomecontrollerParserController.php里搜索{pboot:pageurl},在其下方增加:

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

4、appshomecontrollerIndexController.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);
}

2021-07-26更新补充

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

1、打开appshomecontrollerParserController.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;
        }
    }
}

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;
        }
    }
}

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

打开appshomecontrollerIndexController.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);
}

效果截图:


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

标签:

更新时间:2025-12-07 20:57:03

上一篇:pbootcms文章列表页判读有没有图片的方法

下一篇:pbootcms网站后台使用教程2全局配置配置参数基本配置教程