【網頁圖片加速】pbootcms自動替換圖片地址為七牛云cdn鏡像鏈接
本篇主要是為這篇文章(未備案域名怎么使用國內cdn加速網站?)進行pbootcms相關補充,主要為了解決(jue)網站放在(zai)香港小水(shui)管主機圖片加載太慢問題,或者(zhe)放國內(nei)主機帶寬小且網站圖片多。此(ci)二開(kai)實(shi)現圖片走等提供鏡像存儲的cdn服務商(shang),這樣可(ke)以明顯(xian)提高網站加載速度。
加速原理:圖片后(hou)臺上傳到(dao)本(ben)地(di),然后(hou)前臺訪問網頁的時候圖片會自動鏡像(xiang)一份到(dao)七牛云(yun)上,接(jie)(jie)著返回(hui)七牛云(yun)的圖片鏈接(jie)(jie)到(dao)網頁上顯示(shi),實現圖片加速(su)訪問。
安心提示:此方法是本地依(yi)舊有圖片(pian),七牛云鏡像一份,所(suo)以后期(qi)不用(yong)七牛云了,也不影響網站圖片(pian)加(jia)載(zai)!
修改步驟
1、configconfig.php里增加:(注意前一行(xing)需要(yao)以逗號結尾,默認(ren)官(guan)方版(ban)本無逗號需要(yao)自行(xing)加上)
// cdn鏈接地址,http(s)://img.xxx.com,尾巴不帶“/”,單獨調用{pboot:cdnurl} 'cdn_url' => '',
2、ppshomecontrollerParserController.php里搜索function adjustLabelData,在其下方增(zeng)加:
// 自動替換圖片鏈接 @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、ppshomecontrollerParserController.php里搜索(suo){pboot:pageurl},在其下方(fang)增加:
$content = str_replace('{pboot:cdnurl}', rtrim($this->config('cdn_url'),'/'), $content); // 單獨cdn地址調用標簽 @mk-cdn
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); }
至此,后(hou)面只要去config里添加cdn鏡像鏈接即可。
七牛云鏡像操作參考文章://bilinxiyi.cn/282.html
2021-07-26更新補充
經測試(shi)發現如果前臺圖片(pian)做了裁(cai)剪cdn替(ti)換圖片(pian)地址會失效,因此(ci)還(huan)要做以下調整。
1、打(da)開ppshomecontrollerParserController.php
2、找到(dao)$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;下面增加一行(xing)
if ($cdn_url) $data = str_replace(rtrim($cdn_url,'/'), '', $data);
5、在這(zhe)個判斷if (! file_exists($out_file) && file_exists($src_file)) {結束(shu)的下一行增加
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公共標簽后置解析
把(ba)上(shang)面(mian)代碼下(xia)方(fang)增加:
// 編輯器圖片加cdn @mk-cdn if ($cdn_url = Config::get('cdn_url')) { $content = str_replace('="/static/upload/', '="'.rtrim($cdn_url,'/').'/static/upload/', $content); }
效果截圖:
文章標題:【網頁圖片加速】pbootcms自動替換圖片地址為七牛云cdn鏡像鏈接
文章版權:PB模板網 (bilinxiyi.cn) 所發布的內容,部分為原創文章,轉載請注明來源,網絡轉載文章如有侵權請聯系我們!
本文最后更新發布于
2023-03-17 17:01:28
,某些文章具有時效性,若有錯誤或已失效,請在下方留言或聯系客服: