自动转换AMP版本

master
尹诚诚 7 years ago
parent 014dc67025
commit f94c8a03a8

File diff suppressed because one or more lines are too long

@ -38,17 +38,14 @@ class MY_Output extends CI_Output {
$this->create_folder_by_path(dirname($cache_path));
//如果文件存在,先判断是否为缓存文件,防止覆盖原始程序文件
if (@file_exists($cache_path)) {
if (@file_exists($cache_path) && filesize($cache_path) > 0) {
if (!$fp_read = @fopen($cache_path, FOPEN_READ)) {
return FALSE;
}
flock($fp_read, LOCK_SH);
$cache = '';
if (filesize($cache_path) > 0) {
$cache = fread($fp_read, filesize($cache_path));
}
flock($fp_read, LOCK_UN);
fclose($fp_read);
@ -119,7 +116,6 @@ class MY_Output extends CI_Output {
}
}
}
$html_object->save($cache_path . '.mobile.htm');
}
@ -127,7 +123,102 @@ class MY_Output extends CI_Output {
////// 生成AMP版本
//现在是写在information控制器中
///
//用移动端版本生成
/*
1.加载站点AMP模板模板只有导航、头部、底部
2.删除全部js引用和内链代码
3.删除全部css引用内联css放入<style amp-custom>
4.图片全部替换成amp-img设置宽高设置layout="responsive"
5.去掉所有元素中的style
6.去掉样式表中的!important
7.替换读取源文件title description keywords canonical
8.todo:增加application/ld+json
*/
if (!empty($_GET['amp_optimize'])) {//生成AMP版本
//当前文件夹下的AMP模板
$amp_template=file_get_contents(dirname(__FILE__).'/amp-template.html');
foreach ($html_object->find('title') as $title) {
$amp_template = str_replace('<!--@TITLE@-->', $title->innertext, $amp_template);
}
foreach ($html_object->find('meta') as $meta) {
if($meta->name=='description'){
$amp_template = str_replace('<!--@DESCRIPTION@-->', $meta->content, $amp_template);
}
if($meta->name=='keywords'){
$amp_template = str_replace('<!--@KEYWORDS@-->', $meta->content, $amp_template);
}
}
foreach ($html_object->find('script') as $script) {
$script->outertext = '';
}
foreach ($html_object->find('link') as $link) {
if ($link->rel == 'stylesheet' && !empty($link->href)) {
$link->outertext = '';
}
if ($link->rel == 'canonical' && !empty($link->href)) {
$amp_template = str_replace('<!--@CANONICAL@-->', $link->href, $amp_template);
}
}
$style_content = '';
foreach ($html_object->find('style') as $style) {
$style_content .= $style->innertext;
$style->outertext = '';
}
$style_content = str_replace('!important', '', $style_content);
$amp_template = str_replace('/*@CUSTOM-CSS@*/', $style_content, $amp_template);
//获取图片尺寸AMP要求必须填写width和height
//拼接请求的图片url如 https://data.asiahighlights.com/imagesize.php?photo=/pic/logo-ah.png,/pic/ah-slide-logo.png
$request_size=array();
foreach ($html_object->find('img') as $image) {
$img_src = $image->src;
if (!empty($image->originalsrc)) {
$img_src = $image->originalsrc;
}
$request_size[] = str_replace('https://data.asiahighlights.com', '', $img_src);
}
//请求图片尺寸
$image_sizes=array();
$parse_url='https://data.asiahighlights.com/imagesize.php?photo='.implode(',',$request_size);
$size_data=GET_HTTP($parse_url);
if(!empty($size_data)){
$size_data=json_decode($size_data);
foreach ($size_data as $item){
$item->photo='https://data.asiahighlights.com'.$item->photo;
$image_sizes[$item->photo]=$item;
}
}
//获取图片尺寸 end
foreach ($html_object->find('img') as $image) {
$img_src = $image->src;
if (!empty($image->originalsrc)) {
$img_src = $image->originalsrc;
}
$img_alt = $image->alt;
$img_class = $image->class;
$img_size=$image_sizes[$img_src];
if(!empty($img_size)){
$image->outertext = " <amp-img layout=\"responsive\" class=\"{$img_class}\" alt=\"{$img_alt}\" src=\"{$img_src}\" width=\"{$img_size->width}\" height=\"{$img_size->height}\"></amp-img>";
}
}
foreach ($html_object->find('#content_main') as $content_main) {
$amp_template = str_replace('<!--@CUSTOM-CONENT@-->', $content_main, $amp_template);
}
//替换掉写在组件上的样式
$amp_template = preg_replace('/style=.+?[\'|"]/i', '', $amp_template);
file_put_contents($cache_path . '.amp.htm',$amp_template);
}
////// 生成AMP版本 end

File diff suppressed because one or more lines are too long

@ -1 +1 @@
<?php echo str_replace('textarea##','textarea',$detail->ic_content); ?>
<div id="content_main"><?php echo str_replace('textarea##','textarea',$detail->ic_content); ?></div>
Loading…
Cancel
Save