demo地址:http://www.atool.org/yui.php 支持YUICompressor的所有功能和指令,支持Javascript/Css的在线压缩、混淆和合并文件,从而减少网站网络请求和连接数量。 支持多文件的压缩合并,但是请注
支持YUI Compressor的所有功能和指令,支持Javascript/Css的在线压缩、混淆和合并文件,从而减少网站网络请求和连接数量。
支持多文件的压缩合并,但是请注意合并的文件属于同一种类型,js或css,不可混淆使用。
1. [代码][PHP]代码
function compress() { // read the input foreach ($this->files as $file) { $this->string .= file_get_contents($file) or die("Cannot read from uploaded file"); } // create single file from all input $input_hash = sha1($this->string); $file = $this->TEMP_FILES_DIR . '/' . $input_hash . '.txt'; $fh = fopen($file, 'w') or die("Can't create new file"); fwrite($fh, $this->string); fclose($fh); // start with basic command $cmd = "java -Xmx32m -jar " . escapeshellarg($this->JAR_PATH) . ' ' . escapeshellarg($file) . " --charset UTF-8"; // set the file type $cmd .= " --type " . (strtolower($this->options['type']) == "css" ? "css" : "js"); // and add options as needed if ($this->options['linebreak'] && intval($this->options['linebreak']) > 0) { $cmd .= ' --line-break ' . intval($this->options['linebreak']); } if ($this->options['verbose']) { $cmd .= " -v"; } if ($this->options['nomunge']) { $cmd .= ' --nomunge'; } if ($this->options['semi']) { $cmd .= ' --preserve-semi'; } if ($this->options['nooptimize']) { $cmd .= ' --disable-optimizations'; } // execute the command exec($cmd . ' 2>&1', $raw_output); // add line breaks to show errors in an intelligible manner $flattened_output = implode("\n", $raw_output); // clean up (remove temp file) unlink($file); // return compressed output return $flattened_output; }