01 <?php
02     function CompressFile($FileName) {
03         if(file_exists($FileName)) {
04             $PathInfo = pathinfo($FileName);
05             $BackFileName = str_replace($PathInfo{'extension'}, "bak", $FileName);
06 
07             if(!file_exists($BackFileName) || (filemtime($BackFileName) < filemtime($FileName))) {
08                 if(!rename($FileName, $BackFileName)) {
09                     return FALSE;
10                 }
11 
12                 if(!$BackFileHandle = fopen($BackFileName, "r")) {
13                     die("fopen(ERROR): $BackFileName");
14                 }
15 
16                 $BackFileSize = filesize($BackFileName);
17                 $Content = fread($BackFileHandle, $BackFileSize);
18 
19                 fclose($BackFileHandle);
20 
21                 $Content = mb_ereg_replace("/\*\x20.*?\x20\*/", "", $Content, "m");
22                 $Content = mb_ereg_replace("//\x20.*?\n", "", $Content);
23                 $Content = mb_ereg_replace("#\x20.*?\n", "", $Content);
24 
25                 $Content = mb_ereg_replace("[\t\n]+", "\x20", $Content);
26                 $Content = mb_ereg_replace("\x20+", "\x20", $Content);
27                 $Content = mb_ereg_replace(">\x20<", "><", $Content);
28 
29                 if(!$FileHandle = fopen($FileName, "w")) {
30                     die("fopen(ERROR): $FileName");
31                 }
32 
33                 $FileSize = fwrite($FileHandle, $Content);
34 
35                 fclose($FileHandle);
36 
37                 touch($BackFileName, filemtime($FileName));
38 
39                 printf("<div>compress [%s] %d to %d (%.1f%%)</div>\n", $FileName, $BackFileSize, $FileSize, ($FileSize / $BackFileSize) * 100.0);
40 
41                 return $FileSize;
42             }
43             return FALSE;
44         }
45         return FALSE;
46     }
47 ?>