用 PHP GZIP 压缩 javascripts

上一篇 / 下一篇  2007-11-15 05:06:40 / 个人分类:php

最近正在做的一个程序用了 extjs 实现客户端 UI ,但是 EXTJS 实在是太大了,500k左右的大小在第一次打开时显得特别慢,因此研究用 gzip 来压缩 js 文件,这样传输过程就能减少数据流量, google 了以下,看到有这样一篇文章:

用 PHP 压缩 Javascrīpt

摘录一下这篇文章的代码
<?php
    // check to see if the user has enabled gzip compression in the WordPress admin panel
    if ( ob_get_length() === FALSE and !ini_get('zlib.output_compression') and ini_get('output_handler') != 'ob_gzhandler' and ini_get('output_handler') != 'mb_output_handler' ) {
        ob_start('ob_gzhandler');
    }

    // The headers below tell the browser to cache the file and also tell the browser it is Javascrīpt.
    header("Cache-Control: public");
    header("Pragma: cache");

    $offset = 60*60*24*60;
    $ExpStr = "Expires: ".gmdate("D, d M Y H:i:s",time() + $offset)." GMT";
    $LmStr = "Last-Modified: ".gmdate("D, d M Y H:i:s",filemtime(__FILE__))." GMT";

    header($ExpStr);
    header($LmStr);
    header('Content-Type: text/javascrīpt; charset: UTF-8');
?>


看这篇文章的原理,是将这段代码加入到每个js文件的前面来实现压缩的,但是当js文件太多的时候,就相当累了,改进一下,将上面的文件保存成一个单独的 php 文件(例如 fetchjs.php),当需要引用某一个js的时候可以这样。

http://www.xxx.com/fetchjs.php?scrīpt=file.js

当然还需要对 fetchjs.php 做点完善。最简单就是在后面加上

readfile($_REQUEST['scrīpt']);

更复杂一些自然是需要处理一下js 文件的路径、是否存在等常规检查等工作了。

先写这么多吧。


TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-09-08  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 636
  • 日志数: 10
  • 建立时间: 2007-09-04
  • 更新时间: 2008-04-22

RSS订阅

Open Toolbar