`

PHP 启用Gzip 压缩进行优化

    博客分类:
  • PHP
 
阅读更多
确定服务器支持gzip compression
Use PHP to Gzip CSS files  http://papermashup.com/use-php-to-gzip-css-files/
Compress CSS and JavaScript with PHP http://wp-mix.com/compress-gzip-deflate-css-javascript/
Enabling Gzip Compression of PHP, CSS, and JS Files Without mod_deflate
http://www.warpconduit.net/2010/10/23/enabling-gzip-compression-of-php-css-and-js-files-without-mod_deflate/
Minify helper for cakephp http://bakery.cakephp.org/articles/_k10_/2009/01/17/minify-helper-for-cakephp
Using PHP Minify in CakePHP http://7shifts.com/using-php-minify-in-cakephp/
这篇文章的问题:
1. $min_cachePath = TMP . '/minify';  这里要改为:$min_cachePath = TMP . 'minify';
2. 在app/tmp建立minify文件夹。
3. 修改函数:同时里面的e方法改成echo方法。
function _path($assets, $ext){
        if(!is_array($assets)){
            $assets = array($assets);
        }
        $path = $this->webroot . "min-" . $ext . "?f=";
        foreach($assets as $asset){
            if(strpos($asset,"/")===0){
                //如果以为/开头,表示这个文件不是在规定的地方里面,所以要特殊处理
                $path .= (substr($asset,1) . ".$ext,");
            }else{
                $path .= ($ext . '/' . $asset . ".$ext,");
            }

        }
        return substr($path, 0, count($path)-2);
    }

4.  这里两个变量不存在,要增加app/Vendor/min/config.php
$min_serveOptions['rewriteCssUris'] = false;
$min_serveOptions['minifierOptions']['text/css']['prependRelativePath'] = WEBROOT_URL . 'css/theme/';
5.清理缓存后第一次刷新没问题,第二次刷新就无法下载压缩后的文件,为何?
$path .= ($ext . '/' . $asset . ".$ext"."&rt=".rand(100000,999999)); 这个方法加上随机数,能解决这个问题,但是,客户端能否缓存?




Compress CSS and JavaScript with PHP
CSS, JavaScript131
Quick snippets for compressing CSS and JavaScript with PHP’s ob_gzhandler, which will gzip or deflate content depending on browser support.

Compress CSS content
To compress CSS content, add the following code to any PHP file (i.e., .php extension):
<?php // compress CSS
	header("content-type: text/css; charset: utf-8");
	header("cache-control: must-revalidate");
	$offset = 365 * 24 * 60 * 60;
	$expire = "expires: ".gmdate("D, d M Y H:i:s", time() + $offset)." GMT";
	header($expire);
	if(!ob_start("ob_gzhandler")) ob_start();
?>
 
<?php // replace this line with as much CSS code as you want ?>
 
<?php ob_flush(); ?>

Compress JavaScript content
<?php // compress JS
	header("content-type: text/javascript; charset: UTF-8");
	header("cache-control: must-revalidate");
	$offset = 365 * 24 * 60 * 60;
	$expire = "expires: ".gmdate("D, d M Y H:i:s", time() + $offset)." GMT";
	header($expire);
	if(!ob_start("ob_gzhandler")) ob_start();
?>
 
<?php // replace this line with as much JavaScript code as you want ?>
 
<?php ob_flush(); ?>

That’s all there is to it, and to further reduce file size, you should run the actual CSS/JavaScript code through the minifier of your choice.

For more information about either of these methods, check out my articles at Perishable Press:

Compressed CSS Compression http://perishablepress.com/compressed-css-compression/
Compressed JavaScript Compression http://perishablepress.com/compressed-javascript-compression/
分享到:
评论

相关推荐

    PHP中开启gzip压缩的2种方法

    网页开启gzip压缩以后,其体积可以减小20%~90%,可以节省下大量的带宽,从而减少页面响应时间,提高用户体验。 php配置改法: 复制代码 代码如下: zlib.output_compression = On ;开启gzip功能 zlib.output_...

    php使用curl获取header检测开启GZip压缩的方法

    本文实例讲述了php使用curl获取header检测开启GZip压缩的方法。分享给大家供大家参考,具体如下: 获得网页header信息,是网站开发人员和维护人员常用的技术。网页的header信息,非常丰富,非专业人士一般较难读懂和...

    开启TOMCAT6的GZIP压缩

    开启TOMCAT6的GZIP压缩功能,在客户端请求网页后,从服务器端将网页文件压缩,再下载到客户端,由客户端的浏览器负责解压缩并浏览。相对于普通的浏览过程HTML ,CSS,Javascript , Text ,它可以节省40%左右的流量。...

    php简单开启gzip压缩方法(zlib.output_compression)

    一般而言,页面文件开启gzip压缩以后,其体积可以减小60%~90%,对于文字类站点,可以节省下大量的带宽与用户等待时间。但是不论是iis还是apache默认都只压缩html类静态文件,对于php文件需要模块配置才可支持(iis...

    将PHP程序中返回的JSON格式数据用gzip压缩输出的方法

    2.开启gzip 利用apache mod_deflate module 开启gzip ...3.设置需要gzip压缩输出的类型 json的输出类型是application/json,所以可以这样设置 在httpd.conf的&lt;Directory&gt;&lt;/Directory&gt;中加入 &lt;IfModule m

    php curl中gzip的压缩性能测试实例分析

    主要介绍了php curl中gzip的压缩性能测试,结合实例形式分析了php使用curl的gzip压缩耗时与效率,需要的朋友可以参考下

    PHP中HTTP方式下的Gzip压缩传输方法举偶

    Gzip压缩传输能更加有效节约带宽流量。他先把文本压缩为.gz然后传输给浏览器,最后由浏览器负责解压缩呈现给用户。 老版本的浏览器可能不能显示,但是现在大多数浏览器都能显示。 启用Gzip传输首先要求PHP4.0.5以后...

    使用php判断网页是否gzip压缩

    因为我有过相关经验马上发现是网站开启了gzip而file_get_contents 获得的是压缩过的页面,而不是解压过的页面(不知道是不是要file_get_conttents 请求网页时带上相应的参数,直接获得没有被gzip压缩过的网页?...

    探讨如何在PHP开启gzip页面压缩实例

    本篇文章是对PHP开启gzip页面压缩实例进行了详细的分析介绍,需要的朋友参考下

    apache启用gzip压缩的实现方法

    对于部署在Linux服务器上的PHP程序,在服务器支持的情况下,我们建议你开启使用Gzip Web压缩,以前脚本之家介绍了iis中的开启方法,这篇文章主要介绍了linux中apache的开启方法

    PHP开启gzip页面压缩实例代码

    要实现GZIP压缩页面需要浏览器和服务器共同支持,实际上就是服务器压缩,传到浏览器后浏览器解压并解析。浏览器那边不需要我们担心,因为现在绝大多数浏览器都支持解析GZIP过的页面。我们只要把页面在服务器端压缩再...

    php读取远程gzip压缩网页的方法

    今天在调取一家商城的页面信息时候,使用...类似的信息,即Content-Encoding为gzip,即该站点开启了gzip压缩。这里的解决方案有多种,当然如果你使用file_get_contents的话,可以这样修改:   复制代码 代码如下: file

    php 判断页面或图片是否经过gzip压缩的方法

    使用php判断页面或图片是否经过gzip压缩方法 1.使用get_headers 页面内容 &lt;?php ob_start('ob_gzhandler'); // 开启gzip,屏蔽则关闭 $data = array( array('name'=&gt;'one','value'=&gt;1), array('name'=&gt;'two',...

    使用php判断服务器是否支持Gzip压缩功能

    Gzip可以压缩网页大小从而达到加速打开网页的速度,目前主流的浏览器几乎都支持这个功能,但开启Gzip是需要服务器支持的,在这里我们简单的使用php来判断服务器是否支持Gzip功能。 新建一个php类型文件,可命名为...

Global site tag (gtag.js) - Google Analytics