You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
information-system/application/libraries/html_optimize_lib.php

77 lines
2.4 KiB
PHTML

<?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)
{return $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 (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'];
}
}
//请求图片尺寸
$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;
$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;
}
$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;
}
}
return $html_object->save();
}
}