From a9f70cb70b41b62fab3211bf5092ef68874edbc5 Mon Sep 17 00:00:00 2001 From: ycc Date: Wed, 3 Mar 2021 10:13:37 +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 | 6 ++---- application/libraries/html_optimize_lib.php | 23 +++++++++++++-------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/application/controllers/information.php b/application/controllers/information.php index 4a3efb66..44c969b5 100644 --- a/application/controllers/information.php +++ b/application/controllers/information.php @@ -497,8 +497,6 @@ class Information extends CI_Controller } else if (strcasecmp($site_code, "ch") == 0 && !empty($auto_update_cache)) { //读取模板生成PC和移动优先文件 $information_new = $this->Information_model->Detail($this->input->post('is_id')); - //设置图片尺寸 - $information_new->ic_content=$this->html_optimize_lib->set_image_size($information_new->ic_content); $this->make_www_cache('mobile', $information_new); $this->make_www_cache('pc', $information_new); } else if (strcasecmp($site_code, "cht") == 0 && !empty($auto_update_cache)) { @@ -581,8 +579,6 @@ class Information extends CI_Controller } 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' => '更新成功!')); @@ -728,6 +724,8 @@ class Information extends CI_Controller $full_ic_photo = $this->config->item('site_image_url') . $information->ic_photo; $template = str_replace('', '', $template); } + //设置图片尺寸 + $template=$this->html_optimize_lib->set_image_size($template); // /travelguide/chinese-zodiac/monthly-fortune-for-dog.htm $html_path = $this->config->item('cache')[$this->config->item('site_code')]['cache_path'] . $information->ic_url; $html_path = str_replace("\\", "/", $html_path); diff --git a/application/libraries/html_optimize_lib.php b/application/libraries/html_optimize_lib.php index d5a45d37..f0f9e311 100644 --- a/application/libraries/html_optimize_lib.php +++ b/application/libraries/html_optimize_lib.php @@ -23,7 +23,7 @@ class html_optimize_lib //获取图片尺寸 public function set_image_size($html) - {return $html; + { $html_object = str_get_html($html); //拼接图片链接,获取尺寸 @@ -33,9 +33,10 @@ class html_optimize_lib if (!empty($image->originalsrc)) { $img_src = $image->originalsrc; } - if (strpos($img_src, '//data.') !== false || strpos($img_src, '//images.') !== false) {//以data或者images开头的域名才能获取尺寸 + //图片已经设置了尺寸的不再修改 + if (empty($image->width) && 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']; + $request_size[$img_src_urls['host']][] =$img_src_urls['path']; } } @@ -47,7 +48,7 @@ class html_optimize_lib if (!empty($size_data)) { $size_data = json_decode($size_data); foreach ($size_data as $size_item) { - $size_item->photo = "https://{$host}" . $size_item->photo; + $size_item->photo = "https://".$host . $size_item->photo;//这个作为索引,找到对应url的尺寸 $image_sizes[$size_item->photo] = $size_item; } } @@ -60,12 +61,16 @@ class html_optimize_lib if (!empty($image->originalsrc)) { $img_src = $image->originalsrc; } - $img_src_urls = parse_url($img_src); - $img_src_index="https://".$img_src_urls['host'] . $img_src_urls['path']; - if(!empty($image_sizes[$img_src_index])){ - $image->width=$image_sizes[$img_src_index]->width; - $image->height=$image_sizes[$img_src_index]->height; + //图片已经设置了尺寸的不再修改 + if (empty($image->width) && strpos($img_src, '//data.') !== false || strpos($img_src, '//images.') !== false) {//以data或者images开头的域名才能获取尺寸 + $img_src_urls = parse_url($img_src); + $img_index="https://".$img_src_urls['host'] . $img_src_urls['path']; + if(!empty($image_sizes[$img_index])){ + $image->width=$image_sizes[$img_index]->width; + $image->height=$image_sizes[$img_index]->height; + } } + } return $html_object->save();