From c0e5d62b5f2871247458f7495920b2e80d6f4146 Mon Sep 17 00:00:00 2001 From: ycc Date: Tue, 2 Mar 2021 16:17:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=B9=E9=87=8F=E8=AE=BE=E7=BD=AE=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E5=B0=BA=E5=AF=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/controllers/information.php | 4 ++ application/libraries/html_optimize_lib.php | 74 +++++++++++++++++++++ application/views/mobile_first/ch-next.php | 8 +-- application/views/mobile_first/ch-pc.php | 13 +++- application/views/mobile_first/ch.php | 4 +- 5 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 application/libraries/html_optimize_lib.php diff --git a/application/controllers/information.php b/application/controllers/information.php index f77733c7..fae8a71d 100644 --- a/application/controllers/information.php +++ b/application/controllers/information.php @@ -22,6 +22,7 @@ class Information extends CI_Controller $this->load->model('Infoauthors_model'); $this->load->model('InfoSMS_model'); $this->load->library('Amplib'); //加载AMP处理类 + $this->load->library('html_optimize_lib'); //加载HTML优化类 } public function index() @@ -146,6 +147,7 @@ class Information extends CI_Controller if ($Structure == FALSE) { show_404(); } + //查询结构根节点 $rootStructure = $this->InfoStructures_model->GetParent($Structure->is_path, 1); //没有根节点就从它本身开始查询 @@ -576,6 +578,8 @@ class Information extends CI_Controller return false; } if ($this->html_file_helper('find', $information->ic_url)) { + //设置图片尺寸 + $information->ic_content=$this->html_optimize_lib->set_image_size($information->ic_content); $this->make_www_cache('pc', $information); $this->make_www_cache('mobile', $information); echo json_encode(array('name' => 'yes', 'data' => '更新成功!')); diff --git a/application/libraries/html_optimize_lib.php b/application/libraries/html_optimize_lib.php new file mode 100644 index 00000000..d562c274 --- /dev/null +++ b/application/libraries/html_optimize_lib.php @@ -0,0 +1,74 @@ +CI = &get_instance(); + $this->CI->load->model('Information_model'); + $this->CI->load->model('InfoMetas_model'); + $this->CI->load->library('simple_html_dom_lib'); + } + + + //获取图片尺寸 + public function set_image_size($html) + { + $html_object = str_get_html($html); + + //拼接图片链接,获取尺寸 + $request_size = array(); + foreach ($html_object->find('img') as $image) { + $img_src = $image->src; + if (!empty($image->originalsrc)) { + $img_src = $image->originalsrc; + } + if (strpos($img_src, '//data.') !== false || strpos($img_src, '//images.') !== false) {//以data或者images开头的域名才能获取尺寸 + $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; + } + } + } + + //把尺寸写入html内容中 + foreach ($html_object->find('img') as $image) { + $img_src = $image->src; + if (!empty($image->originalsrc)) { + $img_src = $image->originalsrc; + } + if(!empty($image_sizes[$img_src])){ + $image->width=$image_sizes[$img_src]->width; + $image->height=$image_sizes[$img_src]->height; + } + } + return $html_object; + + //print_r($request_size); + //print_r($image_sizes); + } + + +} diff --git a/application/views/mobile_first/ch-next.php b/application/views/mobile_first/ch-next.php index 50e22639..630fc1f9 100644 --- a/application/views/mobile_first/ch-next.php +++ b/application/views/mobile_first/ch-next.php @@ -5,7 +5,7 @@
<?php echo $info_howtoplan->ic_title; ?> ic_title; ?> @@ -17,7 +17,7 @@
<?php echo $info_besttime->ic_title; ?> ic_title; ?> @@ -29,7 +29,7 @@
<?php echo $info_topthings->ic_title; ?> ic_title; ?> @@ -41,7 +41,7 @@
<?php echo $info_hotel->ic_title; ?> ic_title; ?> diff --git a/application/views/mobile_first/ch-pc.php b/application/views/mobile_first/ch-pc.php index e0fa975c..a7371dcc 100644 --- a/application/views/mobile_first/ch-pc.php +++ b/application/views/mobile_first/ch-pc.php @@ -43,12 +43,14 @@