PHP 启用Gzip 压缩进行优化

确定服务器支持[color=red][b]gzip compression[/b][/color]
Use PHP to Gzip CSS files [url]http://papermashup.com/use-php-to-gzip-css-files/[/url]
Compress CSS and JavaScript with PHP [url]http://wp-mix.com/compress-gzip-deflate-css-javascript/[/url]
[color=red]Enabling Gzip Compression of PHP, CSS, and JS Files Without mod_deflate[/color]
[url]http://www.warpconduit.net/2010/10/23/enabling-gzip-compression-of-php-css-and-js-files-without-mod_deflate/[/url]
[color=red]Minify helper for cakephp[/color] [url]http://bakery.cakephp.org/articles/_k10_/2009/01/17/minify-helper-for-cakephp[/url]
[color=red][b]Using PHP Minify in CakePHP[/b][/color] [url]http://7shifts.com/using-php-minify-in-cakephp/[/url]
这篇文章的问题:
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.[color=red]清理缓存后第一次刷新没问题,第二次刷新就无法下载压缩后的文件,为何?[/color]
$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 [url]http://perishablepress.com/compressed-css-compression/[/url]
Compressed JavaScript Compression [url]http://perishablepress.com/compressed-javascript-compression/[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值