手把手教你做PbootCMS自動替換圖片地址為七牛云cdn鏡像鏈接操作教程

2021-10-13 1931 19 編輯:pbhtml 來源:PB資源網

本篇主要是為這篇文章(未備案域名怎么使用國內cdn加速網站?)進行pbootcms相關補充,主要為了解決網站放在香港小水管主機圖片加載太慢問題,或者放國內主機帶寬小且網站圖片多。此二開實現圖片走七牛云等提供鏡像存儲的cdn服務商,這樣可以明顯提高網站加載速度。

加速原理:圖片后臺上傳到本地,然后前臺訪問網頁的時候圖片會自動鏡像一份到七牛云上,接著返回七牛云的圖片鏈接到網頁上顯示,實現圖片加速訪問。

安心提示:此方法是本地依舊有圖片,七牛云鏡像一份,所以后期不用七牛云了,也不影響網站圖片加載!

如果以下教程不想看,想直接快速一鍵替換文件,請點擊->【一鍵替換文件】PbootCMS自動替換圖片地址為七牛云cdn鏡像鏈接¥6

【一鍵替換文件】是適合不想自行動手的人、手殘黨等,那么可以選擇支持一罐紅牛給站長~

二開增加自動替換圖片地址

1、configconfig.php里增加:(注意前一行需要以逗號結尾,默認官方版本無逗號需要自行加上)

// cdn鏈接地址,http(s)://img.xxx.com,尾巴不帶“/”,單獨調用{pboot:cdnurl}
'cdn_url' => '',

image.png

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

image.png

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

$content = str_replace('{pboot:cdnurl}', rtrim($this->config('cdn_url'),'/'), $content); // 單獨cdn地址調用標簽 @mk-cdn

image.png

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

image.png

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

七牛云鏡像操作參考文章:https://www.pbhtml.com/282.html

2021-07-26更新補充

經測試發現如果前臺圖片做了裁剪cdn替換圖片地址會失效,因此還要做以下調整。

1、打開ppshomecontrollerParserController.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;
        }
    }
}

【網頁圖片加速】PbootCMS自動替換圖片地址為七牛云cdn鏡像鏈接

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

【網頁圖片加速】PbootCMS自動替換圖片地址為七牛云cdn鏡像鏈接

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鏡像鏈接


以上教程看后,如果你有疑問,歡迎加入本站Q群交流討論!


最后更新于  2021-08-23 11:55:14本文來源:https://www.pbhtml.com/288.html略有修改

相關知識點: 七牛云 cdn 免備案 鏡像 網站圖片加速 網頁速度
本站文章均為蜀戎網絡摘自權威資料,書籍,或網絡原創文章,如有版權糾紛或者違規問題,請即刻聯系我們刪除,未經允許禁止復制轉載!感謝...
更多人喜歡

在線
客服

在線客服服務時間:9:00-21:00

客服
熱線

13227777380
7*24小時客服服務熱線

客服
微信

掃一掃微信咨詢
頂部
最近2019年免费中文字幕电影,最近更新2018中文国语字幕,最近中文字幕2018高清一页,一二三四免费观看视频中文版在线宜宾蜀戎网络公司