2012-07-12 169 views
0

任何人都知道需要哪一個選項來壓縮一行中的所有css。我使用的是一個叫做bwp-minify的wordpress插件,但是插件的css壓縮級別很低,我試圖通過YUICompressor.php來改變它。我嘗試將換行符更改爲0和100000,但都沒有影響。壓縮CSS YUI壓縮器

是以下;

--line-break 
     Some source control tools don't like files containing lines longer than, 
     say 8000 characters. The linebreak option is used in that case to split 
     long lines after a specific column. It can also be used to make the code 
     more readable, easier to debug (especially with the MS Script Debugger) 
     Specify 0 to get a line break after each semi-colon in JavaScript, and 
     after each rule in CSS. 

源代碼:

<?php 
/** 
* Class Minify_YUICompressor 
* @package Minify 
*/ 

/** 
* Compress Javascript/CSS using the YUI Compressor 
* 
* You must set $jarFile and $tempDir before calling the minify functions. 
* Also, depending on your shell's environment, you may need to specify 
* the full path to java in $javaExecutable or use putenv() to setup the 
* Java environment. 
* 
* <code> 
* Minify_YUICompressor::$jarFile = '/path/to/yuicompressor-2.3.5.jar'; 
* Minify_YUICompressor::$tempDir = '/tmp'; 
* $code = Minify_YUICompressor::minifyJs(
* $code 
* ,array('nomunge' => true, 'line-break' => 1000) 
*); 
* </code> 
* 
* @todo unit tests, $options docs 
* 
* @package Minify 
* @author Stephen Clay <[email protected]> 
*/ 
class Minify_YUICompressor { 

    /** 
    * Filepath of the YUI Compressor jar file. This must be set before 
    * calling minifyJs() or minifyCss(). 
    * 
    * @var string 
    */ 
    public static $jarFile = null; 

    /** 
    * Writable temp directory. This must be set before calling minifyJs() 
    * or minifyCss(). 
    * 
    * @var string 
    */ 
    public static $tempDir = null; 

    /** 
    * Filepath of "java" executable (may be needed if not in shell's PATH) 
    * 
    * @var string 
    */ 
    public static $javaExecutable = 'java'; 

    /** 
    * Minify a Javascript string 
    * 
    * @param string $js 
    * 
    * @param array $options (verbose is ignored) 
    * 
    * @see http://www.julienlecomte.net/yuicompressor/README 
    * 
    * @return string 
    */ 
    public static function minifyJs($js, $options = array()) 
    { 
     return self::_minify('js', $js, $options); 
    } 

    /** 
    * Minify a CSS string 
    * 
    * @param string $css 
    * 
    * @param array $options (verbose is ignored) 
    * 
    * @see http://www.julienlecomte.net/yuicompressor/README 
    * 
    * @return string 
    */ 
    public static function minifyCss($css, $options = array()) 
    { 
     return self::_minify('css', $css, $options); 
    } 

    private static function _minify($type, $content, $options) 
    { 
     self::_prepare(); 
     if (! ($tmpFile = tempnam(self::$tempDir, 'yuic_'))) { 
      throw new Exception('Minify_YUICompressor : could not create temp file.'); 
     } 
     file_put_contents($tmpFile, $content); 
     exec(self::_getCmd($options, $type, $tmpFile), $output); 
     unlink($tmpFile); 
     return implode("\n", $output); 
    } 

    private static function _getCmd($userOptions, $type, $tmpFile) 
    { 
     $o = array_merge(
      array(
       'charset' => '' 
       ,'line-break' => 5000 
       ,'type' => $type 
       ,'nomunge' => false 
       ,'preserve-semi' => false 
       ,'disable-optimizations' => false 
      ) 
      ,$userOptions 
     ); 
     $cmd = self::$javaExecutable . ' -jar ' . escapeshellarg(self::$jarFile) 
      . " --type {$type}" 
      . (preg_match('/^[a-zA-Z0-9\\-]+$/', $o['charset']) 
       ? " --charset {$o['charset']}" 
       : '') 
      . (is_numeric($o['line-break']) && $o['line-break'] >= 0 
       ? ' --line-break ' . (int)$o['line-break'] 
       : ''); 
     if ($type === 'js') { 
      foreach (array('nomunge', 'preserve-semi', 'disable-optimizations') as $opt) { 
       $cmd .= $o[$opt] 
        ? " --{$opt}" 
        : ''; 
      } 
     } 
     return $cmd . ' ' . escapeshellarg($tmpFile); 
    } 

    private static function _prepare() 
    { 
     if (! is_file(self::$jarFile)) { 
      throw new Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not a valid file.'); 
     } 
     if (! is_dir(self::$tempDir)) { 
      throw new Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not a valid direcotry.'); 
     } 
     if (! is_writable(self::$tempDir)) { 
      throw new Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not writable.'); 
     } 
    } 
} 

回答

0

你有使用--line-break選項試圖

當我使用YUI壓縮機,我不指定選項和輸出是在一行。


從你提供的,無論你叫

Minify_YUICompressor::minifyCss('/path/to.css'); 

您需要的line-break選項這樣設置-1源代碼來看:

Minify_YUICompressor::minifyCss('/path/to.css', array('line-break' => -1)); 
+0

我註釋掉了,但它沒有工作,輸出如下... [鏈接](http://n1bar.com/wp-content/plugins/bwp-minify/min/?f=wp-content/plugins /usernoise/css/button.css,wp-content/plugins/dpSocialBar/css/dpSocialBar.css,wp-content/plugins/special-recent-posts-2.4.7/css/css-front.css,wp-content /plugins/ubermenu/styles/basic.css,wp-content/plugins/ubermenu/styles/skins/blacksilver.css &版本= 1342096143) – 2012-07-12 14:14:18

+0

我下載它,並壓縮它自己沒有--line斷和1得到的一切線。你能把我鏈接到你使用的YUICompressor.php嗎? – Will 2012-07-12 14:20:13

+0

[鏈接](http://wordpress.org/extend/plugins/bwp-minify/) – 2012-07-12 14:21:04

0

把下面的代碼開頭你的PHP腳本的

<?php header('Content-Type:application/javascript'); ?>