1
1
const imagemin = require ( 'imagemin' ) ;
2
2
const imageminOptipng = require ( 'imagemin-optipng' ) ;
3
3
4
+ var execFile = require ( 'child_process' ) . execFile ;
5
+ var pngout = require ( 'pngout-bin' ) ;
6
+
7
+
4
8
var args = process . argv . slice ( 2 ) ;
5
9
if ( args . length < 2 ) {
6
10
console . log ( "Please pass two arguments" ) ;
@@ -13,10 +17,55 @@ if(args.length < 2) {
13
17
var source = args [ 0 ] || './image/' ;
14
18
var destination = args [ 1 ] || './optimized/' ;
15
19
20
+ console . log ( "OptiPNGing all PNGs located in: " + source ) ;
21
+ console . log ( "Output for those will be located in: " + destination ) ;
22
+
16
23
imagemin ( [ source + '/**/*.{jpg,png}' ] , destination , {
17
24
plugins : [
18
25
imageminOptipng ( { optimizationLevel : 3 } ) // default level is enough: 16 trial, could bump up to level 7: 240 trials
19
26
]
20
27
} ) . 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
+ }
22
34
} ) ;
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 ---- */
0 commit comments