AH版自动转换AMP功能上线

hotfix/远程访问多媒体中心
尹诚诚 7 years ago
parent bd0d61c93c
commit 93022f655d

@ -1,22 +1,26 @@
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Index extends CI_Controller {
class Index extends CI_Controller
{
public function __construct() {
public function __construct()
{
parent::__construct();
$this->load->library('simple_html_dom_lib');
}
public function index() {
public function index()
{
$this->load->view('welcome');
}
public function optimize() {
public function optimize()
{
$htmlsource = $this->input->post('htmlsource');
$websitehost = $this->input->post('websitehost');
$create_amp = $this->input->post('create_amp');
if (empty($htmlsource) || empty($websitehost)) {
$this->output->set_status_header(500);
echo 'error:htmlsource or websitehost is empty!';
@ -131,20 +135,164 @@ class Index extends CI_Controller {
$html_object = str_replace('</body>', $lastload_js . '</body>', $html_object);
}
echo json_encode(array('result' => 'ok', 'data' => array('html_object' => $html_object, 'js_content' => $js_content)));
$amp = '';
if (!empty($create_amp)) {
$amp = $this->create_amp($html_object, 'ah',$websitehost);
}
echo json_encode(array('result' => 'ok', 'data' => array('html_object' => $html_object, 'js_content' => $js_content, 'amp' => $amp)));
}
//格式化url保证请求的URL有域名//更换为对应的域名路径
function format_url($url, $host = '') {
function format_url($url, $host = '')
{
if (substr($url, 0, 8) == 'https://' || substr($url, 0, 7) == 'http://') {
$url = str_replace('http://', 'https://', $url);//http要改为https
return urldecode($url);
}
if (substr($url, 0, 2) == '//') { //https或http
return urldecode(str_replace('//', 'http://', $url));
return urldecode(str_replace('//', 'https://', $url));
}
return urldecode($host . $url);
}
//生成AMP版本 用移动端版本生成
/*
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
要求: 图片文件必须带有域名,否则的获取尺寸会出错,如 https://data.asiahighlights.com/pic/abc.jpg
*/
//$htmlsource 移动端版本的HTML对象 $cache_path AMP文件保存路径
function create_amp($htmlsource, $site_code,$websitehost)
{
$html_object = str_get_html($htmlsource);
//读取站点的AMP模板
$amp_template = $this->load->view('amp-template/' . $site_code,null, true);
//生成移动版本,删除多余元素
foreach ($html_object->find('.hidden-xs') as $hidden_item) {
$hidden_item->outertext = '';
}
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('noscript') as $noscript) {
$noscript->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
foreach ($html_object->find('img') as $image) {
if(!empty($image->src)){
$img_src_host = parse_url($image->src,PHP_URL_HOST);
if(empty($img_src_host)){
$img_src_host=$websitehost;
}
$image->src= $this->format_url($image->src,$img_src_host);
}
if (!empty($image->originalsrc)) {
$img_src_host = parse_url($image->src,PHP_URL_HOST);
if(empty($img_src_host)){
$img_src_host=$websitehost;
}
$image->originalsrc = $this->format_url($image->originalsrc,$img_src_host);
}
}
//拼接请求的图片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;
}
$img_src_urls = parse_url($img_src);
$request_size[$img_src_urls['host']][] = $img_src_urls['path'];
}
//请求图片尺寸
$image_sizes = array();
foreach ($request_size as $host => $path) {
$parse_url = "https://{$host}/imagesize.php?photo=" . implode(',', $path);
$size_data = GET_HTTP($parse_url);
if (!empty($size_data)) {
$size_data = json_decode($size_data);
foreach ($size_data as $size_item) {
$size_item->photo = "https://{$host}" . $size_item->photo;
$image_sizes[$size_item->photo] = $size_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);
return $amp_template;
}
}

File diff suppressed because one or more lines are too long

@ -14,7 +14,8 @@
<h1>页面样式精简</h1>
<form name="form_htmlsource" id="form_htmlsource" action="<?php echo site_url('apps/htmlcompressor/index/optimize'); ?>" method="post">
<input type="text" name="websitehost" id="websitehost" style="width:200px;" placeholder="域名" /><br/><br/>
<textarea name="htmlsource" id="htmlsource" rows="22" style="width:800px;" placeholder="网页代码" ></textarea>
<textarea name="htmlsource" id="htmlsource" rows="22" style="width:800px;" placeholder="网页代码" ></textarea><br/><br/>
<label><input type="checkbox" value="true" name="create_amp">生成AMP页面</label><br/><br/>
<input type="submit"/>
</form>

Loading…
Cancel
Save