批量设置图片尺寸

ct-mobile-first
ycc 4 years ago
parent 4b9f42857e
commit a9f70cb70b

@ -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('<!--@OG:IMAGE@-->', '<meta property="og:image" content="' . $full_ic_photo . '"><meta property="og:image:secure_url" content="' . $full_ic_photo . '"><meta property="og:image:width" content="800"><meta property="og:image:height" content="450">', $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);

@ -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();

Loading…
Cancel
Save