Nov
21
从国外的一篇文章中看到的,具体地址忘了,如果有哪位仁兄知道,还望告知,谢谢
之前我也提到过通过gzip来压缩js文件,但是那种方法有不少的缺点,比如不能在客户端缓存,所以每次都需要通过服务端压缩,这是很耗CPU的。
现在这个方法虽不能说是最好的,但性价比是非常高的。特点:
是不是挺不错的,下面来看看实现方法,以ext为例。(要求配置为apache+php,apache开启mod_rewrite,php开启zlib)
先是在根目录下建一个.htaccess文件
然后构建compress.php(先在根目录下建一个cache文件夹)
compress.php代码
// setting variables
$cache = true;
$cachedir = 'cache';
// Determine the directory and file extension
$fn = $_GET['f'];
$t = explode('.', $fn);
$ext = $t[count($t)-1];
switch($ext) {
case 'css':
$type = 'css';
break;
case 'js':
$type = 'javascript';
break;
}
$base = dirname($fn);
// Determine last modification date of the files
$lastmodified = filemtime($fn);
// Send Etag hash
$hash = $lastmodified . '-' . md5($fn);
header ("Etag: \"" . $hash . "\"");
if (
isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"'
){
// Return visit and no modifications, so do not send anything
header ("HTTP/1.0 304 Not Modified");
header ('Content-Length: 0');
} else {
// First time visit or files were modified
if ($cache) {
// Determine supported compression method
$gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
$deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');
// Determine used compression method
$encoding = $gzip ? 'gzip' : ($deflate ? 'deflate' : 'none');
// Check for buggy versions of Internet Explorer
if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') &&
preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i', $_SERVER['HTTP_USER_AGENT'], $matches)) {
$version = floatval($matches[1]);
if ($version < 6)
$encoding = 'none';
if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1'))
$encoding = 'none';
}
// Try the cache first to see if the compressed file is already generated
$cachefile = 'cache-' . $hash . '.' . str_replace('/', '-', $fn) . ($encoding != 'none' ? '.' . $encoding : '');
if (file_exists($cachedir . '/' . $cachefile)) {
if ($fp = fopen($cachedir . '/' . $cachefile, 'rb')) {
if ($encoding != 'none') {
header ("Content-Encoding: " . $encoding);
}
header ("Content-Type: text/" . $type);
header ("Content-Length: " . filesize($cachedir . '/' . $cachefile));
fpassthru($fp);
fclose($fp);
exit;
}
}
}
// Get contents of the files
$content = file_get_contents($fn);
// Send Content-Type
header ("Content-Type: text/" . $type);
if (isset($encoding) && $encoding != 'none') {
//Send compressed contents
$content = gzencode($content, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
header ("Content-Encoding: " . $encoding);
header ('Content-Length: ' . strlen($content));
echo $content;
} else {
// Send regular contents
header ('Content-Length: ' . strlen($content));
echo $content;
}
// Store cache
if ($cache) {
if ($fp = fopen($cachedir . '/' . $cachefile, 'wb')) {
fwrite($fp, $content);
fclose($fp);
}
}
}
?>
大功告成!如果成功调用的话,会在cache下生成一些gzip文件。
Sep
10
为什么要压缩js文件?很明显,压缩后,文件个头小了,下载就快了,网页也就能在更短的时间内呈现在我们的面前,正常情况下,gzip对js的压缩比能达到3:1左右,以ext-all.js为例,源文件大小:463k,经过gzip压缩后的个头为125k,几乎到了4:1。
代码:
//参数可以是数组或者字符串
gzip_file("ext-all.js");
function gzip_file($file_s){
if(!is_array($file_s)){
$files[] = $file_s;
}
else{
$files = $file_s;
}
header("Expires:".gmdate("D, d M Y H:i:s", time()+15360000)."GMT");
header("Cache-Control: max-age=315360000");
$mtime = filemtime($file);
$gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
header("Last-Modified:" . $gmt_mtime);
ob_start('ob_gzhandler');
foreach($files as $file){
$ext = array_pop(explode('.', $file));
switch ($ext){
case 'css':
header("Content-type: text/css");
break;
case 'js' :
header("Content-type: text/javascript");
break;
case 'gif':
header("Content-type: image/gif");
break;
case 'jpg':
header("Content-type: image/jpeg");
break;
case 'png':
header("Content-type: image/png");
break;
default:
header("Content-type: text/plain");
}
echo implode('', file($file));
}
ob_end_flush();
}
?>
假设将上面的代码存储为ext-all.php
那么引用的时候只需
就能实现对ext-all.js的压缩,当然前提是服务端支持zlib。
参考文章:
如何快速呈现我们的文章
gzip压缩js文件
题外话:ext js是个不错的js框架,在UI方面非常漂亮,效果也很出色,但就是js的个头太大,所以用gzip来压缩一下,个头就不会那么恐怖了。
有人说,现将js用软件压缩一下,然后再用gzip压缩一下,这样文件的个头就能最小,其实不然,我做了一个实验,将ext-all.js用软件压缩后变成199k,确实压缩了不少,但是再用gzip压缩的话,压缩比就很小了,结果是149k。
