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, '', $title->innertext, $amp_template); } foreach ($html_object->find('meta') as $meta) { if($meta->name=='description'){ $amp_template = str_replace('', $meta->content, $amp_template); } if($meta->name=='keywords'){ $amp_template = str_replace('', $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('', $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 = " width}\" height=\"{$img_size->height}\">"; } } foreach ($html_object->find('#content_main') as $content_main) { $amp_template = str_replace('', $content_main, $amp_template); } //替换掉写在组件上的样式 $amp_template = preg_replace('/style=.+?[\'|"]/i', '', $amp_template); file_put_contents($cache_path . '.amp.htm',$amp_template); } ////// 生成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; } }