CSS 含相对路径(import、图片)时必须给 minify() 传目标路径,否则相对路径不会按新位置调整;CSS 内联资源默认上限 5kB,大文件需用 setMaxImportSize() 调整;默认内联类型仅 gif/png/jpg/jpeg/svg/apng/avif/webp/woff/woff2,其他类型需 setImportExtensions() 指定
minify
matthiasmullieMIT活跃维护用 PHP 写的 CSS/JavaScript 压缩器,能去空白、删注释、合并文件(含 @import 和内联小资源),并优化常见代码模式,帮网站减小静态资源体积。
CSS & JavaScript minifier, in PHP. Removes whitespace, strips comments, combines files (incl. @import statements and small assets in CSS files), and optimizes/shortens a few common programming patterns.
composer require matthiasmullie/minifyREADME 未说明,纯 PHP 库,无 GPU 或显存要求
PHP 写的 CSS/JS 压缩库,合并文件并去掉空白注释,适合在 PHP 项目里做资源压缩
用 PHP 写的 CSS/JavaScript 压缩器,能去空白、删注释、合并文件(含 @import 和内联小资源),并优化常见代码模式,帮网站减小静态资源体积。
核心能力
- 去除空白与注释,并把多个文件合并成单个输出
- CSS 自动内联 @import 和小体积图片字体为 data URI
- 压缩常见写法,如 #ff0000 转 red、true 转 !0
- 提供 gzip() 直接输出 gzencode 压缩后的内容
- 可设置内联资源大小上限与内联文件类型
容易踩的坑
- 1.3.76最新正式版2026-11-03 · 2026-11-03 11:32 UTC
这个版本没有写更新说明。
- 1.3.752025-06-25 · 2025-06-25 13:14 UTC
这个版本没有写更新说明。
- 1.3.742025-06-12 · 2025-06-12 08:33 UTC
这个版本没有写更新说明。
- 1.3.732024-03-15 · 2024-03-15 11:56 UTC
这个版本没有写更新说明。
- 1.3.722024-03-13 · 2024-03-13 12:02 UTC
这个版本没有写更新说明。
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
Minify
Removes whitespace, strips comments, combines files (incl. @import statements and small assets in CSS files), and optimizes/shortens a few common programming patterns, such as:
JavaScript
object['property']->object.propertytrue,false->!0,!1while(true)->for(;;)
CSS
@import url("http://path")->@import "http://path"#ff0000,#ff00ff->red,#f0f-0px,50.00px->0,50pxbold->700p {}-> removed
And it comes with a huge test suite.
Usage
CSS
use MatthiasMullie\Minify;
$sourcePath = '/path/to/source/css/file.css';
$minifier = new Minify\CSS($sourcePath);
// we can even add another file, they'll then be
// joined in 1 output file
$sourcePath2 = '/path/to/second/source/css/file.css';
$minifier->add($sourcePath2);
// or we can just add plain CSS
$css = 'body { color: #000000; }';
$minifier->add($css);
// save minified file to disk
$minifiedPath = '/path/to/minified/css/file.css';
$minifier->minify($minifiedPath);
// or just output the content
echo $minifier->minify();
JS
// just look at the CSS example; it's exactly the same, but with the JS class & JS files :)
Methods
Available methods, for both CSS & JS minifier, are:
__construct(/* overload paths */)
The object constructor accepts 0, 1 or multiple paths of files, or even complete CSS/JS content, that should be minified. All CSS/JS passed along, will be combined into 1 minified file.
use MatthiasMullie\Minify;
$minifier = new Minify\JS($path1, $path2);
add($path, /* overload paths */)
This is roughly equivalent to the constructor.
$minifier->add($path3);
$minifier->add($js);
minify($path)
This will minify the files' content, save the result to $path and return the resulting content. If the $path parameter is omitted, the result will not be written anywhere.
CAUTION: If you have CSS with relative paths (to imports, images, ...), you should always specify a target path! Then those relative paths will be adjusted in accordance with the new path.
$minifier->minify('/target/path.js');
gzip($path, $level)
Minifies and optionally saves to a file, just like minify(), but it also gzencode()s the minified content.
$minifier->gzip('/target/path.js');
setMaxImportSize($size) (CSS only)
The CSS minifier will automatically embed referenced files (like images, fonts, ...) into the minified CSS, so they don't have to be fetched over multiple connections.
However, for really large files, it's likely better to load them separately (as it would increase the CSS load time if they were included.)
This method allows the max size of files to import into the minified CSS to be set (in kB). The default size is 5.
$minifier->setMaxImportSize(10);
setImportExtensions($extensions) (CSS only)
The CSS minifier will automatically embed referenced files (like images, fonts, ...) into minified CSS, so they don't have to be fetched over multiple connections.
This methods allows the type of files to be specified, along with their data:mime type.
The default embedded file types are gif, png, jpg, jpeg, svg, apng, avif, webp, woff and woff2.
$extensions = array(
'gif' => 'data:image/gif',
'png' => 'data:image/png',
);
$minifier->setImportExtensions($extensions);
Installation
Simply add a dependency on matthiasmullie/minify to your composer.json file if you use Composer to manage the dependencies of your project:
composer require matthiasmullie/minify
Although it's recommended to use Composer, you can actually include these files anyway you want.
License
Minify is MIT licensed.