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

2023-12-08 0 650

本篇主要是为这篇文章(未备案域名怎么使用国内cdn加速网站?)进行pbootcms相关补充,主要为了解决网站放在香港小水管主机图片加载太慢问题,或者放国内主机带宽小且网站图片多。此二开实现图片走七牛云等提供镜像存储的cdn服务商,这样可以明显提高网站加载速度。

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

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

修改步骤

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

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

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

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

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

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

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

【网页图片加速】pbootcms自动替换图片地址为七牛云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);
}

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

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

七牛云镜像操作参考文章: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镜像链接

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 Pbootcms教程 【网页图片加速】pbootcms自动替换图片地址为七牛云cdn镜像链接 https://www.u-9.cn/jiaocheng/pbootcms-jiaocheng/144508.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务