-
Notifications
You must be signed in to change notification settings - Fork 3
/
csscompressor.php
65 lines (61 loc) · 1.72 KB
/
csscompressor.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
<?php
$css_cache_file = 'cache/styles.css';
$css_path = 'css/';
$css_files = array(
"reset.css",
"typography.css",
"base.css",
"sidebar.css",
"menu.css",
"form.css",
"post.css"
);
function update() {
global $css_path, $css_cache_file, $css_files;
if (file_exists($css_cache_file)) {
$cache_time = filemtime($css_cache_file);
foreach ($css_files as $file) {
if (file_exists($css_path.$file)) {
$time = filemtime($css_path.$file);
if ($time > $cache_time) {
return joinCSSFiles();
break;
}
}
}
} else {
return joinCSSFiles();
}
return file_get_contents($css_cache_file);;
}
function compressCSS($file) {
$filedata = file_get_contents($file);
$filedata = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $filedata);
$filedata = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $filedata);
$filedata = str_replace('{ ', '{', $filedata);
$filedata = str_replace(' }', '}', $filedata);
$filedata = str_replace('; ', ';', $filedata);
$filedata = str_replace(', ', ',', $filedata);
$filedata = str_replace(' {', '{', $filedata);
$filedata = str_replace('} ', '}', $filedata);
$filedata = str_replace(': ', ':', $filedata);
$filedata = str_replace(' ,', ',', $filedata);
$filedata = str_replace(' ;', ';', $filedata);
return $filedata;
}
function joinCSSFiles() {
global $css_cache_file, $css_files, $css_path;
$data = '';
foreach ($css_files as $file) {
if (file_exists($css_path.$file)) {
$data .= compressCSS($css_path.$file);
}
}
file_put_contents($css_cache_file, $data);
return $data;
}
ob_start ("ob_gzhandler");
ob_start("compress");
header("Content-type: text/css;charset: UTF-8");
echo update();
?>