龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php Combo Script自动合并/压缩脚本 仿淘宝

时间:2014-09-05 11:00来源:网络整理 作者:网络 点击:
分享到:
Combo Script自动合并/压缩脚本 仿淘宝 Combo Script自动合并/压缩脚本完整代码下载: http://www.ctdisk.com/file/9402163 脚本使用:- 要求php5及以上版本 - 程序在找不到本地文件的情况下,会去指定的

Combo Script自动合并/压缩脚本

完整代码下载: http://www.ctdisk.com/file/9402163

脚本使用:

  • 要求php5及以上版本
  • 程序在找不到本地文件的情况下,会去指定的cdn上找同名文件
  • 程序会自动转义-min文件为源文件,因此要约定-min文件和原文件要成对出现
  • 需要定义combo.php和minify.php中的$YOUR_CDN变量
  • 如果只是合并压缩local文件,则不必重置$YOUR_CDN变量
  • 这里提供cb.php,用来实现tbcdn的开发环境的模拟,apache的配置在cb.php中

合并文件

  • http://yourdomain.com/combo.php?app/js/1.js&app/js/2.js

合并压缩

  • http://yourdomain.com/minify.php?app/js/1.js&app/js/2.js

模拟淘宝CDN

  • http://a.tbcdn.cn/??1.js,2.js
  • http://a.tbcdn.cn/subdir/??1/js,2.js

文件列表:

  • combo.php 合并文件,不压缩
  • minify.php 合并压缩文件
  • cssmin.php 压缩css
  • jsmin.php 压缩js
  • cb.php 淘宝CDN合并文件策略的模拟

css实例

<?php
/* 压缩 */
$MINIFY = true;
/* 默认cdn地址 */
$YOUR_CDN = 'http://a.tbcdn.cn/';

require 'jsmin.php';
require 'cssmin.php';
/**
 * set e-tag cache
 */
function cache($etag){
    $etag = $etag; //标记字符串,可以任意修改
    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 '';
    }  
}

//得到扩展名
function get_extend($file_name) { 
    $extend =explode("." , $file_name); 
    $va=count($extend)-1;
    return $extend[$va]; 
} 

/**
 * logic begin
 */
$files = array();
//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) {
    //最常见的替换规则
    $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,'/')
    );
    //在这里添加转换过头的各种情况
    $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);
    }
    //文件存在
    if(file_exists($k)) {
        $in_str = file_get_contents($k);
        //处理文本
        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{
            //处理非文本
            $files[] = array($in_str);
        }
    }else{
        //文件不存在
        $in_sta = file($YOUR_CDN.$k);
        //文本的处理
        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{
            //非文本的处理
            $files[] = $in_sta;
        }
    }
}

header("Expires: " . date("D, j M Y H:i:s", strtotime("now + 10 years")) ." GMT");
//文本的处理
header($header[$type]);//文件类型
if(preg_match('/js|css/',$type)){
    $result = join("",$files);
}else{
    //非文本的处理
    $result = join("",$files[0]);
}
cache(md5($result));//etag,处理Etag是否多余?
echo $result;
?>
//该片段来自于http://outofmemory.cn
精彩图集

赞助商链接