欢迎来到素材无忧网,按 + 收藏我们
登录 注册 退出 找回密码

图片加载太慢pbootcms自动替换图片地址为七牛云cdn的方法

时间: 2024-02-25 10:03 阅读: 作者:素材无忧网


本篇主要是为这篇文章(未备案域名怎么使用国内cdn加速网站?)进行pbootcms相关补充,主要为了解决网站放在香港小水管主机图片加载太慢问题,或者放国内主机带宽小且网站图片多。此二次开发实现图片走七牛云等提供镜像存储的cdn服务商,这样可以明显提高网站加载速度。
加速原理:图片后台上传到本地,然后前台访问网页的时候图片会自动镜像一份到七牛云上,接着返回七牛云的图片链接到网页上显示,实现图片加速访问。
安心提示:此方法是本地依旧有图片,七牛云镜像一份,所以后期不用七牛云了,也不影响网站图片加载!
二次开发增加自动替换图片地址
1、configconfig.php里增加:(注意前一行需要以逗号结尾,默认官方版本无逗号需要自行加上)

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


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


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

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


至此,后面只要去config里添加cdn镜像链接即可。
更新补充1
经测试发现如果前台图片做了裁剪cdn替换图片地址会失效,因此还要做以下调整。
1、打开ppshomecontrollerParserController.php
找到$maxheight = isset($params['maxheight']) ? $params['maxheight'] : null;下面增加一行

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

在这个判断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;
}
}
}

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

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

在这个判断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;
}
}
}

单页编辑器替换补充
打开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);
}

效果截图:

版权声明: 本站资源均来自互联网或会员发布,如果侵犯了您的权益请与我们联系,我们将在24小时内删除!谢谢!

转载请注明: 图片加载太慢pbootcms自动替换图片地址为七牛云cdn的方法

标签: pbootcms  
相关文章
推荐文章
模板推荐