All Projects → matthiasmullie → Minify

matthiasmullie / Minify

Licence: mit
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.

Programming Languages

javascript
184084 projects - #8 most used programming language
PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to Minify

hasmin
Hasmin - A Haskell CSS Minifier
Stars: ✭ 55 (-96.78%)
Mutual labels:  compression, minifier, css-minifier
EasyCompressor
⚡ A compression library that implements many compression algorithms such as LZ4, Zstd, LZMA, Snappy, Brotli, GZip, and Deflate. It helps you to improve performance by reducing Memory Usage and Network Traffic for caching.
Stars: ✭ 167 (-90.23%)
Mutual labels:  compression, compressor
minformat
gominfmt makes the Go code more compact to aid further compression; revert with gofmt
Stars: ✭ 19 (-98.89%)
Mutual labels:  compression, minifier
Lizard
Lizard (formerly LZ5) is an efficient compressor with very fast decompression. It achieves compression ratio that is comparable to zip/zlib and zstd/brotli (at low and medium compression levels) at decompression speed of 1000 MB/s and faster.
Stars: ✭ 408 (-76.14%)
Mutual labels:  compression, compressor
vbsmin
VBScript minifier
Stars: ✭ 19 (-98.89%)
Mutual labels:  minify, minifier
packtag
A JSP Taglib for delivering minified, combined and gzip-compressed resources (JavaScript and CSS).
Stars: ✭ 22 (-98.71%)
Mutual labels:  compression, minify
Compress Images
Minify size your images. Image compression with extension: jpg/jpeg, svg, png, gif. NodeJs
Stars: ✭ 331 (-80.64%)
Mutual labels:  minify, compression
MinifyAll
A 𝗩𝗦𝗖𝗼𝗱𝗲 𝗺𝗶𝗻𝗶𝗳𝗶𝗲𝗿 for JS, JSON/C, CSS, and HTML, you will love its simplicity! 🌟 𝘾𝙤𝙢𝙥𝙧𝙚𝙨𝙨 and 𝙜𝙯𝙞𝙥 files and folders 📦 Reduce your bundle and file sizes with lightning speed ⚡
Stars: ✭ 54 (-96.84%)
Mutual labels:  minify, minifier
Rust Brotli
Brotli compressor and decompressor written in rust that optionally avoids the stdlib
Stars: ✭ 504 (-70.53%)
Mutual labels:  compression, compressor
Turbopfor Integer Compression
Fastest Integer Compression
Stars: ✭ 520 (-69.59%)
Mutual labels:  compression, compressor
Leanify
lightweight lossless file minifier/optimizer
Stars: ✭ 694 (-59.42%)
Mutual labels:  minify, compression
LZ77-Compressor
A simplified implementation of the LZ77 compression algorithm
Stars: ✭ 70 (-95.91%)
Mutual labels:  compression, compressor
Html Compress Twig
Twig extension for compressing HTML and inline CSS/JS using WyriHaximus/HtmlCompress
Stars: ✭ 72 (-95.79%)
Mutual labels:  minify, compression
imagemin-power-cli
Optimize (compress) images with power using imagemin 💪
Stars: ✭ 13 (-99.24%)
Mutual labels:  minify, minifier
MinifyAllCli
📦 A lightweight, simple and easy npm tool to 𝗺𝗶𝗻𝗶𝗳𝘆 JSON/C, HTML and CSS! Also known as MinifyAll core! ⭐ Usable as 𝑪𝑳𝑰 tool or 𝒊𝒎𝒑𝒐𝒓𝒕𝒂𝒃𝒍𝒆 in TS/JS as a 𝑴𝑶𝑫𝑼𝑳𝑬 🥰
Stars: ✭ 21 (-98.77%)
Mutual labels:  minify, minifier
Csso
CSS minifier with structural optimizations
Stars: ✭ 3,465 (+102.63%)
Mutual labels:  minifier, css-minifier
Compressor
An android image compression library.
Stars: ✭ 6,745 (+294.44%)
Mutual labels:  compression, compressor
opencart-js-css-minifier
JS & CSS Minifier for OpenCart 2.1
Stars: ✭ 13 (-99.24%)
Mutual labels:  css-minifier, js-minifier
Lzbench
lzbench is an in-memory benchmark of open-source LZ77/LZSS/LZMA compressors
Stars: ✭ 490 (-71.35%)
Mutual labels:  compression, compressor
Stdpack.c
Collection of small public domain de/compressors in plain C.
Stars: ✭ 73 (-95.73%)
Mutual labels:  compression, compressor

Minify

Build status Code coverage Code quality Latest version Downloads total License

Donate/Support: Support

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.property
  • true, false -> !0, !1
  • while(true) -> for(;;)

CSS

  • @import url("http://path") -> @import "http://path"
  • #ff0000, #ff00ff -> red, #f0f
  • -0px, 50.00px -> 0, 50px
  • bold -> 700
  • p {} -> 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.

Challenges

If you're interested in learning some of the harder technical challenges I've encountered building this, you probably want to take a look at what I wrote about it on my blog.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].