php 服务器实现js合并压缩

本文介绍了一种使用PHP实现前端JS文件压缩与智能缓存的方法,通过封装压缩类和构建缓存目录,实现高效压缩和减少重复压缩操作,提升网站性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请注明出处^_^

http://blog.youkuaiyun.com/ibcker/article/details/10120755


一般写前端的难免会同时用到好几个js,js一多页面就各种崩溃了,今天无聊尝试了下php实现js的压缩,拿出来和大家分享一下

首先秉承面向对象复用的实现,当然要先google+github一番先

结果是确实有人做了类似的东西,所以嘛,挑一个来用,嗯,我挑这个 https://github.com/rgrove/jsmin-php

这个库压缩起来很简单,基本就JSMin::minify($jsStr)就哦了·

然后就是自己封装一下,让js文件访问能够智能地被压缩成一个文件返回

有了这个出发点,我们假设我要请求1.js 2.js两个文件,如果我请求http://xxx.com/jss.php?jss=1.js,2.js 就行了岂不是就能达到想要的效果了咩~?

ok,就这样开始

首先取参数


define(DIR_CACHE, './cache/'); //cache files dir
define(DIR_JS, './jss/');//jss files dir

$params=trim($_GET['jss']);//get js file list
$list=explode(',',$params);
foreach ($list as $key => &$value) {
	if (!trim($value)) {
		unset($list[$key]);
	}else{
		$value=eregi_replace('\.+/', '', $value);
	}
}

为了防止非法访问上层目录,加上了个正则替换,把./等给过滤了··

然后为了下次请求的时候能够省去再次压缩的时间(压缩js还是挺慢的,经过测试,压缩jquery的源文件就花去了接近一秒,天啊···),必须做个缓存

那就建个cache目录被,然后就是md5一下文件名存一份压缩后的数据

但还有一个问题是js加载的先后是能影响页面最终的效果的,所以····你懂的,得把最终压缩后的单个文件和拼串后的文件都缓存一份,例如再次访问http://xxx.com/jss.php?jss=2.js,1.js我们就不直接取上次缓存的最终文件了,而是取出2.js的压缩文件和1.js的压缩文件进行拼接,然后再缓存一份2.js1.js的副本


代码大概如下


$path=DIR_CACHE.$product;
if (file_exists($path)) {//if exist product cache
	$contents=file_get_contents($path);
	echo $contents;
}else{
	$contents='';
	foreach ($list as $file) {
		$path=DIR_CACHE.md5($file).'.js';
		if (file_exists($path)) {//if exist file cache
			$contents.=file_get_contents($path);
			$contents.=' ';
		}else{
			$path=DIR_JS.$file;
			if(file_exists($path)){
				$temp=JSMin::minify(file_get_contents($path));
				write2cache($temp,$file);
				$contents.=$temp;
				$contents.=' ';
			}
		}
	}
	if ($contents) {
		write2cache($contents,$product);
		echo $contents;
	}
}

然后就是写缓存,这个简单了···直接上代码吧·

function write2cache($contents,$name){
	$name=md5($name).'.js';
	$fh=fopen(DIR_CACHE.$name,"w");
	if ($fh) {
		fwrite($fh, $contents);
		fclose($fh);
		return true;
	}
	return false;
}

好,基本就这样了··经测试效果杠杠的~~


对了,目录下自己创建两个目录,一个是jss,用来放需要压缩的js文件(里面可以再建文件夹分类),另一个是cache,用来缓存压缩后的文件。

结构基本如下



完整代码:

<?
require 'jsmin.php';

define(DIR_CACHE, './cache/'); //cache files dir
define(DIR_JS, './jss/');//jss files dir

header("Content-Type: text/javascript; charset=UTF-8");

$params=trim($_GET['jss']);//get js file list
$list=explode(',',$params);
foreach ($list as $key => &$value) {
	if (!trim($value)) {
		unset($list[$key]);
	}else{
		$value=eregi_replace('\.+/', '', $value);
	}
}

$product=md5(json_encode($list)).'.js';//create product file name for cache

$path=DIR_CACHE.$product;
if (file_exists($path)) {//if exist product cache
	$contents=file_get_contents($path);
	echo $contents;
}else{
	$contents='';
	foreach ($list as $file) {
		$path=DIR_CACHE.md5($file).'.js';
		if (file_exists($path)) {//if exist file cache
			$contents.=file_get_contents($path);
			$contents.=' ';
		}else{
			$path=DIR_JS.$file;
			if(file_exists($path)){
				$temp=JSMin::minify(file_get_contents($path));
				write2cache($temp,$file);
				$contents.=$temp;
				$contents.=' ';
			}
		}
	}
	if ($contents) {
		write2cache($contents,$product);
		echo $contents;
	}
}


function write2cache($contents,$name){
	$name=md5($name).'.js';
	$fh=fopen(DIR_CACHE.$name,"w");
	if ($fh) {
		fwrite($fh, $contents);
		fclose($fh);
		return true;
	}
	return false;
}

  github地址


文件就不传了,过两天把css的压缩也写一起再直接github ·^_^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值