|
|
|
@ -20,8 +20,26 @@ class html_optimize_lib
|
|
|
|
|
$this->CI->load->library('simple_html_dom_lib');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//开启了延迟加载
|
|
|
|
|
public function set_lazy_loader($html, $grey_pic)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
$html_object = str_get_html($html);
|
|
|
|
|
|
|
|
|
|
foreach ($html_object->find('img') as $image) {
|
|
|
|
|
$img_src = $image->src;
|
|
|
|
|
if (!empty($image->originalsrc)) {
|
|
|
|
|
$img_src = $image->originalsrc;
|
|
|
|
|
}
|
|
|
|
|
$image->src = $grey_pic;
|
|
|
|
|
$image->loader = 'lazy';
|
|
|
|
|
$image->originalsrc = $img_src;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $html_object->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取图片尺寸
|
|
|
|
|
//获取图片尺寸,$lazy_loader是否开启延迟加载
|
|
|
|
|
public function set_image_size($html)
|
|
|
|
|
{
|
|
|
|
|
$html_object = str_get_html($html);
|
|
|
|
@ -34,9 +52,9 @@ class html_optimize_lib
|
|
|
|
|
$img_src = $image->originalsrc;
|
|
|
|
|
}
|
|
|
|
|
//图片已经设置了尺寸的不再修改
|
|
|
|
|
if (empty($image->width) && (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(trim($img_src));
|
|
|
|
|
$request_size[$img_src_urls['host']][] =$img_src_urls['path'];
|
|
|
|
|
$request_size[$img_src_urls['host']][] = $img_src_urls['path'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -48,7 +66,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;//这个作为索引,找到对应url的尺寸
|
|
|
|
|
$size_item->photo = "https://" . $host . $size_item->photo;//这个作为索引,找到对应url的尺寸
|
|
|
|
|
$image_sizes[$size_item->photo] = $size_item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -62,12 +80,12 @@ class html_optimize_lib
|
|
|
|
|
$img_src = $image->originalsrc;
|
|
|
|
|
}
|
|
|
|
|
//图片已经设置了尺寸的不再修改
|
|
|
|
|
if (empty($image->width) && (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(trim($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;
|
|
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|