通过gzip压缩js、css文件,并在客户端缓存

文章分类:PHP | 共4 条评论  查看次数:657 + 123

从国外的一篇文章中看到的,具体地址忘了,如果有哪位仁兄知道,还望告知,谢谢

之前我也提到过通过gzip来压缩js文件,但是那种方法有不少的缺点,比如不能在客户端缓存,所以每次都需要通过服务端压缩,这是很耗CPU的。

现在这个方法虽不能说是最好的,但性价比是非常高的。特点:

  • 只在服务端压缩一次,前提是文件没有人为更新
  • 在客户端缓存(IE,FF)
  • 可以非常方便地压缩所有调用的

是不是挺不错的,下面来看看实现方法,以为例。(要求配置为apache+php,apache开启mod_rewrite,php开启zlib)
先是在根目录下建一个.htaccess文件

RewriteEngine On
RewriteRule (.*.|.*.) compress.php?f=$1 [L]
 

然后构建compress.php(先在根目录下建一个cache文件夹)
compress.php代码

<?php

    // setting variables
    $cache = true;
    $cachedir = 'cache';
   
    // Determine the directory and file extension
    $fn = $_GET['f'];
    $t = explode('.', $fn);
    $ = $t[count($t)-1];
    switch($) {
        case '':
            $type = '';
        break;
        case '':
            $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
            $ = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], '');
            $deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');

            // Determine used compression method
            $encoding = $ ? '' : ($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, $ ? 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来压缩js文件

文章分类:misc | 发表评论  查看次数:588 + 110

为什么要压缩文件?很明显,压缩后,文件个头小了,下载就快了,网页也就能在更短的时间内呈现在我们的面前,正常情况下,的压缩比能达到3:1左右,以-all.为例,源文件大小:463k,经过压缩后的个头为125k,几乎到了4:1。

代码:

<?php
    //参数可以是数组或者字符串
    gzip_file("-all.");

    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){
    $ = array_pop(explode('.', $file));
    switch ($){
    case '':
     header("Content-type: text/");
     break;
    case '' :
     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();
}
?>
 

假设将上面的代码存储为-all.php
那么引用的时候只需

include("-all.php")

就能实现对-all.的压缩,当然前提是服务端支持zlib。

参考文章:
如何快速呈现我们的文章
gzip压缩js文件

题外话:ext js是个不错的框架,在UI方面非常漂亮,效果也很出色,但就是的个头太大,所以用来压缩一下,个头就不会那么恐怖了。

有人说,现将用软件压缩一下,然后再用压缩一下,这样文件的个头就能最小,其实不然,我做了一个实验,将-all.用软件压缩后变成199k,确实压缩了不少,但是再用压缩的话,压缩比就很小了,结果是149k。