php实现压缩css/javascript
2012-09-22 18:41
381 查看
PHP程序实现CSS和js文件的压缩!!!
网址:http://www.dewen.org/q/1424/php%E7%A8%8B%E5%BA%8F%E5%AE%9E%E7%8E%B0css%E6%96%87%E4%BB%B6%E5%92%8Cjs%E6%96%87%E4%BB%B6%E7%9A%84%E5%8E%8B%E7%BC%A9#11634
+------------------------------------------------------------+
优化网站寻找css和js压缩并正常进行!
在存放CSS的文件夹里面新建一个style.php,在此文件夹里面加入以下代码:
<?php
if(extension_loaded('zlib')){
//检查服务器是否开启了zlib扩展
ob_start('ob_gzhandler');
}
header("content-type:text/css;charset:gb2312;");//注意修改你的编码
header("cache-control:must-revalidate");
$offset=60*60*24;//css文件的距离现在过期时间,这里设置为一天
$expire="expire:".gmate("D,d M Y H:i:s",time()+$offset)."GMT";
header($expire);
ob_start(compress);
function compress($buffer){
//去除文件中的注释和空行
$patterns=array('!/\*[^*]*+([^/][^*]*\*)*/!','/\[\s]]*\r/',);
$buffer=preg_replace($patterns,'',$buffer);
return $buffer;
}
//包含你的全部css文档
include ('global.css');
include ('layout.css');
if(extension_loaded('zlib')){
ob_end_flush();//输出buffer中的内容,即压缩后的css文件
}
如果你处理的是javascript文件,你需要将上面代码中的第五行的Content-type修改成以下:
header("content-type:application/x-javascript;charset;gb2312;");
同样需要注意的是文件的编码,这里我用的是gb2312,如果你采用的是utf-8的编码方式,修改对应的即可.如:
<script type="text/javascript" src="http://www.pp43.com/scripts/autoSuggest.js.php?v=221"></script>
由于上面使用了HTTP的Expires(过期)属性用于在客户端缓存CSS/JS代码,所以如果过期时间设置的太长比如(2020),当你的服务器修改了JS/CSS代码时,客户端可能不会立即生效.解决的方法是:在php文件后面添加一个随机参数,如上面例子中的v=121,当下次修改了文件的时候,记得相应修改此随机参数即可.
+------------------------------------------------------------+
<?php
ob_start("ob_gzhandler");
header("Content-type:text/css;charset:UTF-8");
header("Expires:".gmdate("D,d M Y H:i:s",time()+60*60)."GMT");
include('common.css');
echo "\n\n";
include('another.css');
echo "\n\n";
ob_flush();
?>
+------------------------------------------------------------+
提供一个程序:
<?php
/**
* 类WebPuts用于浏览器直接输出数据(例如下载文件)
**/
class WebPuts{
/*
所有要输出的内容
*
*
*@var array
*/
protected $_output=array();
/*
输出文件名
@var string
*/
protected $_output_filename;
/*
输出类型
@var string
*/
protected $_mime_type;
/*
输出文件名的字符集
@var string
*/
protected $_filename_charset='utf-8';
/*
允许客户端输出的文件
@var boolean
*/
protected $_enabled_client_cache=true;
/*
构造函数
@param string $output_filename
@param string $mime_type
@param string $content
*/
function __construct($output_filename,$mime_type='application/octet-stream',$content=null){
$this->_output_filename=$output_filename;
$this->_mime_type =$mime_type;
if($content){
$this->appendData($content);
}
}
/*
添加一个要输出的文件夹
@param string $filename
@return WebPuts
*/
function addFile($filename){
$this->_output[]=array('file',$filename);
return $this;
}
/*
追加要输出的数据
@param string $content;
@return WebPuts
*/
function appendData($content){
$this->_output[]=array('raw',$content);
return $this;
}
/*
设置输出文件名
@param string $output_filename
@return WebPuts
*/
function setOutputFilenameCharset($charset){
$this->_filename_charset=$charset;
return $this;
}
/*
设置是否允许客户端缓存输出的文件
@param boolean $enabled
@return WebPuts
*/
function enableClientCache($enabled=true){
$this->_enabled_client_cache=$enabled;
return $this;
}
/*
设置输出类型
@param string $mime_type
@return WebPuts
*/
function setMimeType($mime_type)
{
$this->_mime_type=$mime_type;
return $this;
}
/*
执行响应
*/
function execute(){
header("Content-Type:{$this->_mime_type}");
$filename='"'.htmlspecialchars($this->_output_filename).'"';
$filesize=0;
foreach($this->_output as $output){
list($type,$data)=$output;
if($type == 'file'){
$filesize+=filesize($data);
}else{
$filesize+=strlen($data);
}
}
header("Content-Disposition:attachment;filename={$filename};charset={$this->_filename_charset}");
if($this->_enabled_client_cache){
header('Pragma:cache');
}
header('Cache-Control:public,must-revalidate,max-age=0');
header("Content-Length:{$filesize});
foreach($this->_output as $output){
list($type,$data)=$output;
if($type=='file'){
readfile($data);
}else{
echo $data;
}
}
}
}
+------------------------------------------------------------+
网址:http://www.dewen.org/q/1424/php%E7%A8%8B%E5%BA%8F%E5%AE%9E%E7%8E%B0css%E6%96%87%E4%BB%B6%E5%92%8Cjs%E6%96%87%E4%BB%B6%E7%9A%84%E5%8E%8B%E7%BC%A9#11634
+------------------------------------------------------------+
优化网站寻找css和js压缩并正常进行!
在存放CSS的文件夹里面新建一个style.php,在此文件夹里面加入以下代码:
<?php
if(extension_loaded('zlib')){
//检查服务器是否开启了zlib扩展
ob_start('ob_gzhandler');
}
header("content-type:text/css;charset:gb2312;");//注意修改你的编码
header("cache-control:must-revalidate");
$offset=60*60*24;//css文件的距离现在过期时间,这里设置为一天
$expire="expire:".gmate("D,d M Y H:i:s",time()+$offset)."GMT";
header($expire);
ob_start(compress);
function compress($buffer){
//去除文件中的注释和空行
$patterns=array('!/\*[^*]*+([^/][^*]*\*)*/!','/\[\s]]*\r/',);
$buffer=preg_replace($patterns,'',$buffer);
return $buffer;
}
//包含你的全部css文档
include ('global.css');
include ('layout.css');
if(extension_loaded('zlib')){
ob_end_flush();//输出buffer中的内容,即压缩后的css文件
}
如果你处理的是javascript文件,你需要将上面代码中的第五行的Content-type修改成以下:
header("content-type:application/x-javascript;charset;gb2312;");
同样需要注意的是文件的编码,这里我用的是gb2312,如果你采用的是utf-8的编码方式,修改对应的即可.如:
<script type="text/javascript" src="http://www.pp43.com/scripts/autoSuggest.js.php?v=221"></script>
由于上面使用了HTTP的Expires(过期)属性用于在客户端缓存CSS/JS代码,所以如果过期时间设置的太长比如(2020),当你的服务器修改了JS/CSS代码时,客户端可能不会立即生效.解决的方法是:在php文件后面添加一个随机参数,如上面例子中的v=121,当下次修改了文件的时候,记得相应修改此随机参数即可.
+------------------------------------------------------------+
<?php
ob_start("ob_gzhandler");
header("Content-type:text/css;charset:UTF-8");
header("Expires:".gmdate("D,d M Y H:i:s",time()+60*60)."GMT");
include('common.css');
echo "\n\n";
include('another.css');
echo "\n\n";
ob_flush();
?>
+------------------------------------------------------------+
提供一个程序:
<?php
/**
* 类WebPuts用于浏览器直接输出数据(例如下载文件)
**/
class WebPuts{
/*
所有要输出的内容
*
*
*@var array
*/
protected $_output=array();
/*
输出文件名
@var string
*/
protected $_output_filename;
/*
输出类型
@var string
*/
protected $_mime_type;
/*
输出文件名的字符集
@var string
*/
protected $_filename_charset='utf-8';
/*
允许客户端输出的文件
@var boolean
*/
protected $_enabled_client_cache=true;
/*
构造函数
@param string $output_filename
@param string $mime_type
@param string $content
*/
function __construct($output_filename,$mime_type='application/octet-stream',$content=null){
$this->_output_filename=$output_filename;
$this->_mime_type =$mime_type;
if($content){
$this->appendData($content);
}
}
/*
添加一个要输出的文件夹
@param string $filename
@return WebPuts
*/
function addFile($filename){
$this->_output[]=array('file',$filename);
return $this;
}
/*
追加要输出的数据
@param string $content;
@return WebPuts
*/
function appendData($content){
$this->_output[]=array('raw',$content);
return $this;
}
/*
设置输出文件名
@param string $output_filename
@return WebPuts
*/
function setOutputFilenameCharset($charset){
$this->_filename_charset=$charset;
return $this;
}
/*
设置是否允许客户端缓存输出的文件
@param boolean $enabled
@return WebPuts
*/
function enableClientCache($enabled=true){
$this->_enabled_client_cache=$enabled;
return $this;
}
/*
设置输出类型
@param string $mime_type
@return WebPuts
*/
function setMimeType($mime_type)
{
$this->_mime_type=$mime_type;
return $this;
}
/*
执行响应
*/
function execute(){
header("Content-Type:{$this->_mime_type}");
$filename='"'.htmlspecialchars($this->_output_filename).'"';
$filesize=0;
foreach($this->_output as $output){
list($type,$data)=$output;
if($type == 'file'){
$filesize+=filesize($data);
}else{
$filesize+=strlen($data);
}
}
header("Content-Disposition:attachment;filename={$filename};charset={$this->_filename_charset}");
if($this->_enabled_client_cache){
header('Pragma:cache');
}
header('Cache-Control:public,must-revalidate,max-age=0');
header("Content-Length:{$filesize});
foreach($this->_output as $output){
list($type,$data)=$output;
if($type=='file'){
readfile($data);
}else{
echo $data;
}
}
}
}
+------------------------------------------------------------+
相关文章推荐
- php实现压缩多个CSS与JS文件的方法
- php+javascript+css 实现动态添加数据行
- 随手做的一个用PHP压缩javascript,css的小脚本
- 利用YaHoo YUI实现Javascript CSS 压缩 分类: C# 2014-07-13 19:07 371人阅读 评论(0) 收藏
- 利用YaHoo YUI实现Javascript CSS 压缩
- php实现压缩多个CSS与JS文件的方法
- javascript实现的样式表(CSS) 格式整理与压缩
- javascript实现的样式表(CSS) 格式整理与压缩
- php压缩HTML函数轻松实现压缩html/js/Css及注意事项
- php压缩HTML函数轻松实现压缩html/js/Css及注意事项
- PHP程序实现CSS和js文件的压缩
- PHP+HTML+JavaScript+Css实现简单爬虫开发
- 各种参考手册地址(Ado、HTML、XHTML、CSS、XML、JavaScript、PHP、ASP、SQL)
- php实现批量压缩图片文件大小的脚本
- JavaScript和CSS通过expression实现Table居中显示
- Javascript+css 实现网页换肤功能
- javascript css红色经典选项卡效果实现代码
- JavaScript+CSS实现网页换肤功能
- php合并加载多个javascript、css文件原理
- javascript创建css、js,onload触发callback兼容主流浏览器的实现