龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php_jscss压缩合并实例

时间:2014-07-22 14:49来源: 作者: 点击:
分享到:
本程序来自fanwe系统. br / 花了点时间拔了下来 br / 做成一个实例的样式,非常简单 br / 希望对此有需要的phper有用 br / jscss压缩合并有利于减少请求线程数.加快网页加载速度 br /
本程序来自fanwe系统.
花了点时间拔了下来
做成一个实例的样式,非常简单
希望对此有需要的phper有用
jscss压缩合并有利于减少请求线程数.加快网页加载速度

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>压缩合并</title>
<?php
$pagecss[] = "css/appcenter.css";
$pagecss[] = "css/dialog.css";
$pagecss[] = "css/default_blue.css";
$pagecss[] = "css/default_yp_blue.css";
$cssfile=parse_css($pagecss,"./static/",".");
?>
<link rel="stylesheet" type="text/css" href="<?=$cssfile?>" />
<?php
$pagejs[] = "js/content_addtop.js";
$pagejs[] = "js/admin_common.js";
$pagejs[] = "js/colorpicker.js";
$pagejs[] = "js/comparison.js";
$jsfile=parse_script($pagejs,"./static/",".");
?>
<script type="text/javascript" src="<?=$jsfile?>"></script>
</head>

<body>
</body>
</html>
<?php
function parse_css($urls,$path="./static/",$tmpl_path='.')
{
	$url = md5(implode(',',$urls));
	$css_url = $path.$url.'.css';
	if(!file_exists($css_url))
	{
		if(!file_exists($path))mkdir($path,0777);
		$css_content = '';
		
		foreach($urls as $url)
		{$css_content .= @file_get_contents($url);}
		
		$css_content = preg_replace("/[\r\n]/",'',$css_content);
		$css_content = str_replace("../images/",$tmpl_path."/images/",$css_content);
		@file_put_contents($css_url,$css_content);
	}
	return $css_url;
}
function parse_script($urls,$path="./static/")
{
	$url = md5(implode(',',$urls));
	$js_url = $path.$url.'.js';
	if(!file_exists($js_url))
	{
		if(!file_exists($path))mkdir($path,0777);
		require_once "inc/javascriptpacker.php";
		$js_content = '';
		foreach($urls as $url)
		{
			$append_content = @file_get_contents($url)."\r\n";
			$packer = new JavaScriptPacker($append_content);
			$append_content = $packer->pack();
			$js_content .= $append_content;
		}
		@file_put_contents($js_url,$js_content);
	}
	return $js_url;
}
?>

2. [文件] javascriptpacker.php ~ 24KB     下载(73)     [全屏预览]

精彩图集

赞助商链接