diff --git a/application/core/MY_Output-页面改造版本.php b/application/core/MY_Output-页面改造版本.php new file mode 100644 index 00000000..cb244954 --- /dev/null +++ b/application/core/MY_Output-页面改造版本.php @@ -0,0 +1,392 @@ +create_folder_by_path(dirname($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 = fread($fp_read, filesize($cache_path)); + + flock($fp_read, LOCK_UN); + fclose($fp_read); + if (strpos($cache, ' +
+
+ + +
+ + + +
+
+ CUSTOMIZE
+
+ + + +
+
+
+ + + +
+ + +
+ + +EOT; + + foreach ($html_object->find('#header') as $headerbar) { + $headerbar->outertext = $amp_navbar; + } + + //替换底部 +$amp_footer=<< + + + +
+ + +EOT; + + foreach ($html_object->find('#footer') as $footer) { + $footer->outertext = $amp_footer; + } + + //获取图片尺寸,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_source = preg_replace('/style=".*"?/i', '', $html_object); + $html_object = str_get_html($amp_source); + $html_object->save($cache_path . '.amp.htm'); + } + + ////// 生成AMP版本 end + + if (flock($fp, LOCK_EX)) { + fwrite($fp, $output . ''); + flock($fp, LOCK_UN); + } else { + log_message('error', "Unable to secure a file lock for file at: " . $cache_path); + return; + } + fclose($fp); + @chmod($cache_path, FILE_WRITE_MODE); + + log_message('debug', "Cache file written: " . $cache_path); + } + + // -------------------------------------------------------------------- + + /* + * 递归创建文件夹 + */ + function create_folder_by_path($dir, $mode = 0777) { + if (!is_dir($dir)) { + return @mkdir($dir, $mode, true); + } + return; + } + + + //获取网络图片尺寸 + function GetImageSize($url) { + $dataBlock=file_get_contents ($url , 0 , NULL , 0 , 168); + if (!$dataBlock){ + return false; + } + // 将读取的图片信息转化为图片路径并获取图片信息,经测试,这里的转化设置 jpeg 对获取png,gif的信息没有影响,无须分别设置 + // 有些图片虽然可以在浏览器查看但实际已被损坏可能无法解析信息 + $size = getimagesize('data://image/jpeg;base64,'. base64_encode($dataBlock)); + if (empty($size)) { + return false; + } + //$result['width'] = $size[0]; + //$result['height'] = $size[1]; + return $size; + } + +} diff --git a/application/core/MY_Output.php b/application/core/MY_Output.php index d150a930..de22dfa8 100644 --- a/application/core/MY_Output.php +++ b/application/core/MY_Output.php @@ -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)); - } + $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放入 + + + + + + + + +
+
+ + +
+ + + + +
+ + + +
+
+
+ + + +
+ + +
+ + + + + +
+ + + + diff --git a/application/views/information_none.php b/application/views/information_none.php index 2bb24f4e..587ecde5 100644 --- a/application/views/information_none.php +++ b/application/views/information_none.php @@ -1 +1 @@ -ic_content); ?> \ No newline at end of file +
ic_content); ?>
\ No newline at end of file