项目在线压缩js

<?php
/*$src = 'js/myjs.js';
$out = 'js/myjs.min.js';
require 'class.JavaScriptPacker.php';

$script = file_get_contents($src);

$t1 = microtime(true);

$packer = new JavaScriptPacker($script, 'Normal', true, false);
$packed = $packer->pack();

$t2 = microtime(true);
$time = sprintf('%.4f', ($t2 - $t1) );
//echo 'script ', $src, ' packed in ' , $out, ', in ', $time, ' s.', "\n";
file_put_contents($out, $packed);
*/
require 'class.JavaScriptPacker.php';
/**
* 在线压缩js函数
* @param string $src 压缩文件路径,项目下的相对路径
* @param string $packerPath 压缩后的新文件名,项目下的相对路径,可以为空
* @return string $packerPath
* autor:yuzhonghe113@126.com
*/

function jsPacker($src,$packerPath=""){
//$path=dirname(__FILE__);
if(!file_exists($src)) exit("File Is Not Exist");
$fileName=basename($src,'.js');//文件名
$filePath=dirname($src);//文件路径
if(empty($packerPath)){
$packerPath=$filePath."/".$fileName.".min.js";//压缩后的新文件名
}
//print_R(filemtime($packerPath)."|".filemtime($src));exit;
if(file_exists($packerPath)&& filemtime($packerPath)>filemtime($src)){
return $packerPath;
}else{
$script = file_get_contents($src);
//$t1 = microtime(true);
$packer = new JavaScriptPacker($script, 'Normal', true, false);
$packed = $packer->pack();
//$t2 = microtime(true);
//$time = sprintf('%.4f', ($t2 - $t1) );
//echo 'script ', $src, ' packed in ' , $out, ', in ', $time, ' s.', "\n";
file_put_contents($packerPath, $packed);
}
return $packerPath;
}
?>

文件下载

posted @ 2013-05-03 12:54  hechunhua  阅读(444)  评论(0编辑  收藏  举报