|
|
|
|
<?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_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) {
|
|
|
|
|
$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();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|