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

原创
2016-07-25 09:02:17 585浏览
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实例

js实例

php文件编码统一使用utf-8
  1. /* 压缩 */
  2. $MINIFY = true;
  3. /* 默认cdn地址 */
  4. $YOUR_CDN = 'http://a.tbcdn.cn/';
  5. require 'jsmin.php';
  6. require 'cssmin.php';
  7. /**
  8. * set e-tag cache
  9. */
  10. function cache($etag){
  11. $etag = $etag; //标记字符串,可以任意修改
  12. if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag){
  13. header('Etag:'.$etag,true,304);
  14. exit;
  15. }
  16. else header('Etag:'.$etag);
  17. }
  18. function get_contents($url){
  19. $ch =curl_init($url);
  20. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  21. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  22. $str =curl_exec($ch);
  23. curl_close($ch);
  24. if ($str !==false) {
  25. return $str;
  26. }else {
  27. return '';
  28. }
  29. }
  30. //得到扩展名
  31. function get_extend($file_name) {
  32. $extend =explode("." , $file_name);
  33. $va=count($extend)-1;
  34. return $extend[$va];
  35. }
  36. /**
  37. * logic begin
  38. */
  39. $files = array();
  40. //cdn上存在的各种可能的文件类型
  41. $header = array(
  42. 'js' => 'Content-Type: application/x-javascript',
  43. 'css' => 'Content-Type: text/css',
  44. 'jpg' => 'Content-Type: image/jpg',
  45. 'gif' => 'Content-Type: image/gif',
  46. 'png' => 'Content-Type: image/png',
  47. 'jpeg' => 'Content-Type: image/jpeg',
  48. 'swf' => 'Content-Type: application/x-shockwave-flash'
  49. );
  50. $type = '';
  51. foreach ($_GET as $k => $v) {
  52. //最常见的替换规则
  53. $k = preg_replace(
  54. array('/_(js|css|gif|png|jpg|jpeg|swf)$/','/yui\/2_8_0r4/','/yui\/3_0_0/','/(\d+)_(\d+)_(\d+)/','/(\d+)_(\d+)/','/_v(\d)/'),
  55. array('.$1','yui/2.8.0r4','yui/3.0.0','$1.$2.$3','$1.$2','.v$1'),
  56. trim($k,'//m.sbmmt.com/m/')
  57. );
  58. //在这里添加转换过头的各种情况
  59. $k = str_replace('global.v5.css','global_v5.css',$k);
  60. $k = str_replace('detail.v2.css','detail_v2.css',$k);
  61. $k = str_replace('cubee_combo','cubee.combo',$k);
  62. if(empty($type)) {
  63. $type = get_extend($k);
  64. }
  65. //文件存在
  66. if(file_exists($k)) {
  67. $in_str = file_get_contents($k);
  68. //处理文本
  69. if(preg_match('/js|css/',$type)){
  70. //$files[] = file_get_contents($k);
  71. if($MINIFY == true && $type == 'js'){
  72. $files[] = JSMin::minify($in_str);
  73. }else if($MINIFY == true && $type == 'css'){
  74. $files[] = cssmin::minify($in_str);
  75. }else{
  76. $files[] = $in_str;
  77. }
  78. }else{
  79. //处理非文本
  80. $files[] = array($in_str);
  81. }
  82. }else{
  83. //文件不存在
  84. $in_sta = file($YOUR_CDN.$k);
  85. //文本的处理
  86. if(preg_match('/js|css/',$type)){
  87. $files[] = '/* http://a.tbcdn.cn/'.$k.' */';
  88. $inner_str = join('',$in_sta);
  89. if($MINIFY == true && $type == 'js'){
  90. $files[] = JSMin::minify($inner_str);
  91. }else if($MINIFY == true && $type == 'css'){
  92. $files[] = cssmin::minify($inner_str);
  93. }else{
  94. $files[] = $inner_str;
  95. }
  96. }else{
  97. //非文本的处理
  98. $files[] = $in_sta;
  99. }
  100. }
  101. }
  102. header("Expires: " . date("D, j M Y H:i:s", strtotime("now + 10 years")) ." GMT");
  103. //文本的处理
  104. header($header[$type]);//文件类型
  105. if(preg_match('/js|css/',$type)){
  106. $result = join("",$files);
  107. }else{
  108. //非文本的处理
  109. $result = join("",$files[0]);
  110. }
  111. cache(md5($result));//etag,处理Etag是否多余?
  112. echo $result;
  113. ?>
复制代码


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:360防跨站攻击脚本 下一条:js传递参数给php有乱码

相关文章

查看更多