-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathminify.php
79 lines (75 loc) · 1.8 KB
/
minify.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
header("Content-Type:text/html;charset=utf-8");
/* 压缩 */
$MINIFY = true;
/* 默认cdn地址 */
$YOUR_CDN = 'http://cdn.iceinto.com/';
require 'jsmin.php';
require 'cssmin.php';
/**
* 根据地址获取文件内容
* @param $url
* @return mixed|string
*/
function get_contents($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$str = curl_exec($ch);
curl_close($ch);
if ($str !== false) {
return $str;
} else {
return '';
}
}
/**
* 得到扩展名
* @param $file_name
* @return mixed
*/
function get_extend($file_name)
{
$extend = explode(".", $file_name);
$va = count($extend) - 1;
return $extend[$va];
}
$files = array();
//只处理js 和 css
$header = array(
'js' => 'Content-Type: application/x-javascript',
'css' => 'Content-Type: text/css',
);
//文件类型
$type = '';
foreach ($_GET as $k => $v) {
$k = str_replace(array('_'), array('.'), $k);
if (empty($type)) {
$type = get_extend($k);
}
//文件不存在
$inner_str = file_get_contents($YOUR_CDN . $k);
//文本的处理
if (preg_match('/js|css/', $type) && $inner_str) {
if ($MINIFY == true && $type == 'js') {
$files[] = JSMin::minify($inner_str);
} else if ($MINIFY == true && $type == 'css') {
$files[] = cssmin::minify($inner_str);
} else {
$files[] = $inner_str;
}
}
}
header("Expires: " . date("D, j M Y H:i:s", strtotime("now + 10 years")) . " GMT");
if (!empty($type)) {
header($header[$type]); //文件类型
if (preg_match('/js|css/', $type)) {
$result = implode("", $files);
} else {
//非文本的处理
$result = implode("", $files[0]);
}
echo $result;
}
?>