设为首页 收藏本站
查看: 975|回复: 0

[经验分享] php gizp压缩传输js和css文件

[复制链接]

尚未签到

发表于 2017-3-26 11:45:00 | 显示全部楼层 |阅读模式
  实例:

<?php
/**
*  完整调用示例:
*  1、combine.php?t=j&b=public&fs=jslib.jquery,function
*  
*  该例子调用的是<a href="http://www.sharetk.com/" target="_blank"><u>网站</u></a>根目录下的public/jslib/jquery.js和public/function.js
*  
*  2、combine.php?t=j&fs=jslib.jquery,function
*  
*  该例子调用的是<a href="http://www.sharetk.com/" target="_blank"><u>网站</u></a>根目录下的jslib/jquery.js和function.js
*  
*  3、combine.php?t=c&b=public.css&fs=common,index
*  
*  该例子调用的是网站根目录下的public/css/common.css和public/css/index.css
*  
*  4、combine.php?t=c&fs=css.common
*  该例子调用的是网站根目录下的css/common.css
*  
*  注:多个文件名之间用,分隔;只有一个文件名最后不要有,
*     用,分隔的多个文件会被压缩进一个文件,一次性传给<a href="http://www.sharetk.com/" target="_blank"><u>浏览器</u></a>
**/
$is_bad_request=false;
$cache = true;
$doc_root_uri=$_SERVER['DOCUMENT_ROOT'].'/';
$cachedir = $doc_root_uri . 'public/cache';
//文件类型,j为js,c为css
$type=isset($_GET['t'])?($_GET['t']=='j'||$_GET['t']=='c'?$_GET['t']:''):'';
//存放js和css文件的基目录, 例如:?b=public.js 代表的是/public/js文件夹,出发点是网站根目录
//基目录参数不是必须的,如果有基目录那么这个基目录就会附加在文件名之前
$base =isset($_GET['b'])?($doc_root_uri.str_replace('.','/',$_GET['b'])):$doc_root_uri;
//文件名列表,文件名不带后缀名.比如基目录是
//文件名的格式是 :基目录(如果有)+文件包名+文件名
//例如:类型是j,
//     文件名public.js.jquery
//     如果有基路径且为public,
//     那么转换后的文件名就是/public/public/js/jquery.js
//     如果没有基路径
//     那么转换后的文件名就是/public/js/jquery.js
//多个文件名之间用,分隔
$fs=isset($_GET['fs'])?str_replace('.','/',$_GET['fs']):'';
$fs=str_replace(',','.'.($type=='j'?'js,':'css,'),$fs);
$fs=$fs.($type=='j'?'.js':'.css');

if($type==''||$fs==''){$is_bad_request=true;}
//die($base);
///////////////http://blog.ddian.cn
if($is_bad_request){header ("HTTP/1.0 503 Not Implemented");}

$file_type=$type=='j'?'javascript':'css';
$elements = explode(',',preg_replace('/([^?]*).*/', '\\1', $fs));
// Determine last modification date of the files
$lastmodified = 0;
while (list(,$element) = each($elements)) {
$path =$base . '/' . $element;
if (($type == 'j' && substr($path, -3) != '.js') ||
($type == 'c' && substr($path, -4) != '.css')) {
header ("HTTP/1.0 403 Forbidden");
exit;   
}
if (substr($path, 0, strlen($base)) != $base || !file_exists($path)) {
header ("HTTP/1.0 404 Not Found");
exit;
}
$lastmodified = max($lastmodified, filemtime($path));
}
// Send Etag hash
$hash = $lastmodified . '-' . md5($fs);
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-Type: text/" . $file_type);
header ('Content-Length: 0');
}
else
{
// First time visit or files were modified
if ($cache)
{
// Determine supported compression method
$gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
$deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');
// Determine used compression method
$encoding = $gzip ? 'gzip' : ($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 combined files were already generated
$cachefile = 'cache-' . $hash . '.' . $file_type . ($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/" . $file_type);
header ("Content-Length: " . filesize($cachedir . '/' . $cachefile));
fpassthru($fp);
fclose($fp);
exit;
}
}
}
// Get contents of the files
$contents = '';
reset($elements);
while (list(,$element) = each($elements)) {
$path = $base . '/' . $element;
$contents .= "\\n\\n" . file_get_contents($path);
}
// Send Content-Type
header ("Content-Type: text/" . $file_type);
if (isset($encoding) && $encoding != 'none')
{
// Send compressed contents
$contents = gzencode($contents, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
header ("Content-Encoding: " . $encoding);
header ('Content-Length: ' . strlen($contents));
echo $contents;
}
else
{
// Send regular contents
header ('Content-Length: ' . strlen($contents));
echo $contents;
}
// Store cache
if ($cache) {
if ($fp = fopen($cachedir . '/' . $cachefile, 'wb')) {
fwrite($fp, $contents);
fclose($fp);
}
}
}
  源于:http://www.sharetk.com/html/code/php/7707.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-355587-1-1.html 上篇帖子: PHP 之编程初体验(一) 下篇帖子: PHP 之session cookie
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表