Combo Script automatically merges/compresses scripts
Complete code download:
http://www.ctdisk.com/file/9402163
Script usage:
- Requires php5 and above
- If the program cannot find a local file, it will go to the specified cdn to find the file with the same name.
- The program will automatically escape the -min file as a source file, so it is agreed that the -min file and the original file must appear in pairs
- Need to define the $YOUR_CDN variable in combo.php and minify.php
- If you just merge and compress local files, you don’t have to reset the $YOUR_CDN variable
- cb.php is provided here to simulate the development environment of tbcdn. The configuration of apache is in cb.php
Merge files
- http://yourdomain.com/combo.php?app/js/1.js&app/js/2.js
Merge and compress
- http://yourdomain.com/minify.php?app/js/1.js&app/js/2.js
Simulate Taobao CDN
- http://a.tbcdn.cn/??1.js,2.js
- http://a.tbcdn.cn/subdir/??1/js,2.js
File list:
- combo.php combines files without compression
- minify.php merges compressed files
- cssmin.php compress css
- jsmin.php compresses js
- cb.php Simulation of Taobao CDN file merging strategy
css example
js example
PHP file encoding uses utf-8 uniformly
/* Compression*/ $MINIFY = true; /* Default cdn address*/ $YOUR_CDN = 'http://a.tbcdn.cn/'; require 'jsmin .php'; require 'cssmin.php'; /** * set e-tag cache */ function cache($etag){ $etag = $etag; //Tag string, can be modified at will if ($_SERVER ['HTTP_IF_NONE_MATCH'] == $etag){ header('Etag:'.$etag,true,304); exit; } else header('Etag:'.$etag); } 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 ''; } } //Get the extension function get_extend($file_name) { $extend =explode("." , $file_name); $va=count($extend)-1; return $extend[$va]; } /** * logic begin */ $files = array(); //Various possible file types that exist on cdn $header = array( 'js' => 'Content-Type: application/x-javascript',
'css' => ' Content-Type: text/css',
'jpg' => 'Content-Type: image/jpg',
'gif' => 'Content-Type: image/gif',
'png' => 'Content-Type: image/png',
'jpeg' => 'Content-Type: image/jpeg',
'swf' => 'Content-Type: application/x-shockwave-flash'
);
$type = '';
foreach ($_GET as $k => $v) {
//The most common replacement rules
$k = preg_replace(
array('/_(js|css|gif|png |jpg|jpeg|swf)$/','/yui/2_8_0r4/','/yui/3_0_0/','/(d+)_(d+)_(d+)/','/(d+)_( d+)/','/_v(d)/'),
array('.$1','yui/2.8.0r4','yui/3.0.0','$1.$2.$3','$1. $2','.v$1'),
trim($k,'/')
);
//Add various cases of excessive conversion here
$k = str_replace('global.v5.css',' global_v5.css',$k);
$k = str_replace('detail.v2.css','detail_v2.css',$k);
$k = str_replace('cubee_combo','cubee.combo',$ k);
if(empty($type)) {
$type = get_extend($k);
}
//The file exists
if(file_exists($k)) {
$in_str = file_get_contents($k) ;
//Process text
if(preg_match('/js|css/',$type)){
//$files[] = file_get_contents($k);
if($MINIFY == true && $type = = 'js'){
$files[] = JSMin::minify($in_str);
}else if($MINIFY == true && $type == 'css'){
$files[] = cssmin:: minify($in_str);
}else{
$files[] = $in_str;
}
}else{
//Processing non-text
$files[] = array($in_str);
}
}else{
//The file does not exist
$in_sta = file($YOUR_CDN.$k);
//Text processing
if(preg_match('/js|css/',$type)){
$files[] = '/ * http://a.tbcdn.cn/'.$k.' */';
$inner_str = join('',$in_sta);
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;
}
}else{
//Non-text processing
$files[] = $in_sta;
}
}
}
header("Expires: " . date("D, j M Y H:i:s", strtotime("now + 10 years")) ." GMT");
//Text processing
header($header[$type]);// File type
if(preg_match('/js|css/',$type)){
$result = join("",$files);
}else{
//Non-text processing
$result = join( "",$files[0]);
}
cache(md5($result));//etag, is it redundant to process Etag?
echo $result;
?>
Copy code