Skip to content

Commit e3efa15

Browse files
committed
Adds extra step to losslessly compress with PNGout
1 parent b83108d commit e3efa15

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed

lossless.js

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const imagemin = require('imagemin');
22
const imageminOptipng = require('imagemin-optipng');
33

4+
var execFile = require('child_process').execFile;
5+
var pngout = require('pngout-bin');
6+
7+
48
var args = process.argv.slice(2);
59
if(args.length < 2) {
610
console.log("Please pass two arguments");
@@ -13,10 +17,55 @@ if(args.length < 2) {
1317
var source = args[0] || './image/';
1418
var destination = args[1] || './optimized/';
1519

20+
console.log("OptiPNGing all PNGs located in: " + source);
21+
console.log("Output for those will be located in: " + destination);
22+
1623
imagemin([source + '/**/*.{jpg,png}'], destination, {
1724
plugins: [
1825
imageminOptipng({optimizationLevel : 3}) // default level is enough: 16 trial, could bump up to level 7: 240 trials
1926
]
2027
}).then(files => {
21-
console.log("Compressed losslessly, find files in " + destination);
28+
if (files.length > 0) {
29+
console.log("Now 2nd pass on each compressed image, with PNGOut this time");
30+
pngoutit(0, files);
31+
} else {
32+
console.log("No file to pngout, DONE.");
33+
}
2234
});
35+
36+
var pngoutit = function(index, files) {
37+
console.log("PNGouting: " + files[index].path);
38+
execFile(pngout, [files[index].path, files[index].path + '.min.png', '-s0', '-k0', '-f0'], function (err) {
39+
console.log('OK');
40+
index++;
41+
if (index >= files.length) {
42+
console.log("DONE.");
43+
} else {
44+
pngoutit(index, files);
45+
}
46+
});
47+
}
48+
49+
/* ---- PNG options: ----
50+
51+
/f0 None
52+
/f1 Sub (delta x)
53+
/f2 Up (delta y)
54+
/f3 Average (delta x&y)
55+
/f4 Paeth
56+
/f5 Adaptive (mixed)
57+
/f6 Reuse (copy line by line from source PNG)
58+
For palette images /f0 is best, and /f5 is often the best for grayscale or true color images, but not always.
59+
The default value is /f0 for palette images (/c3), and /f5 for everything else.
60+
61+
/s0 Xtreme! (Slowest)
62+
/s1 Intense (Slow)
63+
/s2 Longest Match (Fast)
64+
/s3 Huffman Only (Faster)
65+
/s4 Uncompressed (Fastest)
66+
67+
/k0 Remove all unnecessary chunks. (default)
68+
/kp Keep the palette order intact. Using this may hurt compression slightly,
69+
but it may be necessary for certain applications (often on small electronic devices) that need to share a common palette.
70+
71+
---- PNG options ---- */

lossly-then-losslessly.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const imageminPngquant = require('imagemin-pngquant');
33
const imageminOptipng = require('imagemin-optipng');
44

55
const fs = require("fs");
6+
var execFile = require('child_process').execFile;
7+
var pngout = require('pngout-bin');
68

79
var args = process.argv.slice(2);
810
if(args.length < 3) {
@@ -30,14 +32,25 @@ imagemin([source + '/**/*.{jpg,png}'], destination, {
3032
]
3133
}).then(files => {
3234
console.log("Optimized PNGs now Compressed, find files in " + finalDestination);
35+
if (files.length > 0) {
36+
console.log("Now 2nd pass on each compressed image, with PNGOut this time");
37+
pngoutit(0, files);
38+
} else {
39+
console.log("No file to pngout, DONE.");
40+
}
3341
});
3442

3543
});
3644

37-
38-
39-
40-
41-
42-
43-
45+
var pngoutit = function(index, files) {
46+
console.log("PNGouting: " + files[index].path);
47+
execFile(pngout, [files[index].path, '-s0', '-k0', '-f0'], function (err) {
48+
console.log('OK');
49+
index++;
50+
if (index >= files.length) {
51+
console.log("DONE.");
52+
} else {
53+
pngoutit(index, files);
54+
}
55+
});
56+
}

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@
2323
},
2424
"homepage": "https://github.com/hirako2000/png-compressor#readme",
2525
"dependencies": {
26+
"gm": "^1.22.0",
2627
"imagemin": "^5.1.2",
28+
"imagemin-mozjpeg": "^6.0.0",
2729
"imagemin-optipng": "^5.1.0",
28-
"imagemin-pngquant": "^5.0.0"
30+
"imagemin-pngquant": "^5.0.0",
31+
"imagemin-zopfli": "^4.2.0",
32+
"mkdirp": "^0.5.1",
33+
"pngout-bin": "^3.0.0"
2934
}
3035
}

0 commit comments

Comments
 (0)