|
|
<?php
|
|
|
|
|
|
if (!defined('BASEPATH'))
|
|
|
exit('No direct script access allowed');
|
|
|
|
|
|
/*
|
|
|
* HTML的优化处理的类库
|
|
|
*/
|
|
|
|
|
|
class html_optimize_lib
|
|
|
{
|
|
|
|
|
|
var $CI;
|
|
|
|
|
|
public function __construct()
|
|
|
{
|
|
|
$this->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_lazy_loader($html, $grey_pic)
|
|
|
{
|
|
|
|
|
|
$html_object = str_get_html($html);
|
|
|
|
|
|
foreach ($html_object->find('img') as $image) {
|
|
|
$img_src = $image->src;
|
|
|
|
|
|
/**有些图片不需要延迟加载,比如头部第一张图,延迟加载会影响CLS的评分,所以图片加loader="nolazy"属性就可以不用替换为延迟加载代码。 */
|
|
|
$loader = $image->loader;
|
|
|
if (!empty($loader) && $loader == "nolazy") {
|
|
|
continue;
|
|
|
}
|
|
|
/** 无需延迟加载结束 20210527 zp */
|
|
|
|
|
|
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);
|
|
|
|
|
|
//拼接图片链接,获取尺寸
|
|
|
$request_size = array();
|
|
|
foreach ($html_object->find('img') as $image) {
|
|
|
$img_src = $image->src;
|
|
|
if (!empty($image->originalsrc)) {
|
|
|
$img_src = $image->originalsrc;
|
|
|
}
|
|
|
//图片已经设置了尺寸的不再修改
|
|
|
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'];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//请求图片尺寸
|
|
|
$image_sizes = array();
|
|
|
foreach ($request_size as $host => $path) { //拼接的URL有长度限制,大量的图片一次性请求就会失败,需要分批处理
|
|
|
$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;//这个作为索引,找到对应url的尺寸
|
|
|
$image_sizes[$size_item->photo] = $size_item;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//print_r($request_size);
|
|
|
//print_r($image_sizes);
|
|
|
//把尺寸写入html内容中
|
|
|
foreach ($html_object->find('img') as $image) {
|
|
|
$img_src = $image->src;
|
|
|
if (!empty($image->originalsrc)) {
|
|
|
$img_src = $image->originalsrc;
|
|
|
}
|
|
|
//图片已经设置了尺寸的不再修改
|
|
|
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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
return $html_object->save();
|
|
|
}
|
|
|
|
|
|
//获取图片尺寸的本地版本,图片存在相同服务器上,速度比较快
|
|
|
//$dataPath data文件夹本地路径
|
|
|
//$imagesPath images文件夹本地路径
|
|
|
public function set_image_size_local($html, $dataPath, $imagesPath)
|
|
|
{
|
|
|
$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;
|
|
|
}
|
|
|
//图片已经设置了尺寸的不再修改
|
|
|
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'];
|
|
|
$file_path = '';
|
|
|
if (strpos($img_src, '//data.') !== false) {
|
|
|
$file_path = $dataPath . $img_src_urls['path'];
|
|
|
}
|
|
|
if (strpos($img_src, '//images.') !== false) {
|
|
|
$file_path = $imagesPath . $img_src_urls['path'];
|
|
|
}
|
|
|
if (is_file($file_path)) {
|
|
|
$properties = getimagesize($file_path);//获取图片属性,读取失败返回false
|
|
|
if ($properties && !empty($properties[0]) && !empty($properties[1])) {
|
|
|
$image->width = $properties[0];
|
|
|
$image->height = $properties[1];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return $html_object->save();
|
|
|
}
|
|
|
|
|
|
///查询html的标签,然后后面添加新html
|
|
|
public function add_htmlcode($html,$findTag,$addHtml){
|
|
|
$html_object = str_get_html($html);
|
|
|
$findE = $html_object->find($findTag,0);
|
|
|
$findE->outertext = $findE->outertext . $addHtml ;
|
|
|
return $html_object->save();
|
|
|
$html_object->clear();
|
|
|
}
|
|
|
|
|
|
}
|