我们知道,在html的页面中,所有空格和换行符其实都会占据一定的空间,即使使用了gzip压缩,在传输过程中依然会浪费用户的流量和我们自己服务器的带宽,此脚本就是为了解决这个问题而诞生的。

请自行下载Google Closure Compiler,和YUIcompressor
点此去下载Google Closure Compiler
点此去下载YUIcompressor

将compiler.jar和yuicompressor-2.4.7.jar放在与本脚本相同的目录下即可
代码很简单,没什么好说的。。
enjoy it!

<?php
/**
* 代码压缩处理
*
* 1.压缩模板文件,去掉所有换行和头尾空格
* 2.压缩js和css文件,生成同名.min.js或.min.css文件
*
* @author 废墟 <r.anerg#gmail.com>
* @version 1.1
* @link http://anerg.com
*
*/
ini_set("memory_limit", -1);
ini_set("display_error", 1);
error_reporting(E_ALL);
date_default_timezone_set('Asia/Shanghai'); define('BASEPATH', dirname(__FILE__));
class Compress_Template { private $input_dir; //模板源目录
private $output_dir; //压缩后的模板目录 public function __construct($type) {
$this->input_dir = dirname(BASEPATH) . '/application/views_src/';
$this->output_dir = dirname(BASEPATH) . '/application/views/';
$this->js_dir = dirname(BASEPATH) . '/wwwroot/static/js/';
$this->css_dir = dirname(BASEPATH) . '/wwwroot/static/css/';
} public function run() {
exec('rm ' . $this->output_dir . ' -rf');
$this->compress_file($this->input_dir, $this->output_dir);
$this->compress_js_css();
} private function compress_js_css() {
exec('for JS in $(find ' . $this->js_dir . ' -type f -name "*.js"|grep -v "min.js"); do java -jar compiler.jar --charset=UTF-8 --compilation_level=SIMPLE_OPTIMIZATIONS --js=$JS --js_output_file=${JS%%.js}.min.js; done');
exec('for CSS in $(find ' . $this->css_dir . ' -type f -name "*.css"|grep -v "min.css"); do java -jar yuicompressor-2.4.7.jar --type css --charset utf-8 -o ${CSS%%.css}.min.css $CSS; done');
} private function compress_file($input, $output) {
$input = rtrim($input, DIRECTORY_SEPARATOR);
$output = rtrim($output, DIRECTORY_SEPARATOR);
if (is_dir($input)) {
$input .= DIRECTORY_SEPARATOR;
$output .= DIRECTORY_SEPARATOR;
foreach (glob($input . "*") as $path) {
$compress_path = str_replace($input, $output, $path);
if (is_dir($path)) {
if (!is_dir($compress_path)) {
$this->mkpath($compress_path);
}
$this->compress_file($path, $compress_path);
}
if (is_file($path)) {
$data = array_map("trim", file($path));
file_put_contents($compress_path, join('', $data));
}
}
}
} private function mkpath($path) {
return is_dir($path) or ($this->mkpath(dirname($path)) and (mkdir($path, 0777) and chmod($path, 0777)));
}
} $app = new Compress_Template();
$app->run();
echo "OK!\n";
 

最新文章

  1. ORACLE 自定义聚合函数
  2. C++ pair用法
  3. Java Performance Optimization Tools and Techniques for Turbocharged Apps--reference
  4. ie6,7下js动态加载图片不显示错误
  5. 快速搭建本地HTTP服务器
  6. python中重要的模块--asyncio
  7. Java 8 Date-Time API 详解
  8. MyBatis - 介绍、简单入门程序
  9. codeforces476D
  10. [Swift]LeetCode598. 范围求和 II | Range Addition II
  11. 清华机试中手机键盘问题求解 java 和 c
  12. 微信昵称的emoji的尝试性解决方案
  13. doy09 文件处理,拷贝文件
  14. Working days
  15. java8 集合神操作
  16. kernel dump Analysis
  17. 【cs229-Lecture2】Gradient Descent 最小二乘回归问题解析表达式推导过程及实现源码(无需迭代)
  18. h5请求跨域问题Access-Control-Allow-Origin解决跨域
  19. python面试十题
  20. oracle进阶之分析函数

热门文章

  1. Android错误之--Error retrieving parent for item: No resource found that matches the given name &amp;#39;Theme.A
  2. 效果超赞的基于js的chat组件和图形动画组件
  3. 怎样封装RESTful Web Service
  4. Hive优化策略
  5. SpringBoot使用MyBatis报错:Error invoking SqlProvider method (tk.mybatis.mapper.provider.base.BaseInsertProvider.dynamicSQL)
  6. 查看/设置MySQL数据库的事务隔离级别
  7. Hadoop-2.6.0上调用C的API实现相似云盘的功能
  8. Redis用LPUSH和RPOP实现消息队列
  9. Swift中文教程(七)--协议,扩展和泛型
  10. hdu 5881 Tea (2016 acm 青岛网络赛)