来源:
http://aciddrop.com/2008/01/21/b ... th-3-lines-of-code/
翻译:西山一片元
转载请注明出处!
http://www.pmal.net
There are 4 relatively easy ways by which you can speed up the time it takes a browser to download a page:
有四个 有效的方法能够让你的浏览器下载网页变得更快
* Make fewer HTTP requests //减少HTTP请求
* Add a far-future expires header //加载一个过期的头文件
* Gzip your page's components //压缩你的网页组件
* Minify your JavaScript, CSS and HTML //修改您的js/css/html
Following on from my post on joining CSS and JavaScript files, I have written a PHP script which will automatically do all of the above. All you have to do is call the following at the top of your page:
我写了一个PHP脚本来自动 压缩我的CSS和JS文件,你也可以这样做,首先在的文件头嵌入以下代码:
require_once('class.compressor.php'); //Include the class. The full path may be required
$compressor = new compressor('css,javascript,page');
And the following at the bottom of the page:
然后在程序尾部,写下以下代码:
打完收工!!
需要注意的地方是:
* A server running at least PHP4. For JavaScript minification to work as well, PHP5+ is required.
//服务器运行的PHP必须至少是PHP4以上的,如果要让JS压缩得更好,要求PHP版本是5以上
* You should set the folder where you are running the class from as writable
//你需要设置您要运行这个类的目录为可读写权限
基本设置:
$compressor = new compressor('css,javascript,page');
The string can contain 'css', 'javascript' and 'page' depending on which elements you would like to be compressed. Any element contained in the string will be gzip encoded and minified.
//字符串可以包含CSS、javascript和PAGE等你想压缩的组件。包含在这个数组里的组件都会被压缩
Alternatively, an array can be passed to the class constructor with an advanced set of options. This array would set all the compression options to on:
$compressor = new compressor(array("javascript"=>array("cachedir"=>'/minify',
"gzip"=>true,
"minify"=>true,
),
"css"=>array("cachedir"=>'/wp-content',
"gzip"=>true,
"minify"=>true,
),
"page"=>array("gzip"=>true,
"minify"=>true
)
));
压缩效果:
压缩前:
压缩后
类文件
[attach]98678[/attach]