Created
October 6, 2017 21:45
-
-
Save sobstel/19ac8218d6c5b9bc115020d906d2bc5f to your computer and use it in GitHub Desktop.
Optimal images optimization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Optimizes all images | |
// (takes only those that have no corresponding @2x) | |
// | |
const im = require('imagemagick'); | |
const glob = require('glob'); | |
const fs = require('fs'); | |
const { exec } = require('child_process'); | |
function args (inputPath, outputPath, width) { | |
return [ | |
inputPath, | |
'-filter', 'Triangle', | |
'-define', 'filter:support=2', | |
'-thumbnail', width, | |
'-unsharp', '0.25x0.25+8+0.065', | |
'-dither', 'None', | |
'-posterize', 136, | |
'-quality', 82, | |
'-define', 'jpeg:fancy-upsampling=off', | |
'-define', 'png:compression-filter=5', | |
'-define', 'png:compression-level=9', | |
'-define', 'png:compression-strategy=1', | |
'-define', 'png:exclude-chunk=all', | |
'-interlace', 'none', | |
'-colorspace', 'sRGB', | |
'-strip', | |
outputPath | |
]; | |
} | |
glob('src/images/**/*@3x.*', (er, files) => { | |
files.forEach((file3x) => { | |
const file2x = file3x.replace('@3x', '@2x'); | |
if (!fs.existsSync(file2x)) { | |
// @2x | |
im.convert(args(file3x, file2x, 750), () => { | |
console.log(`${file3x} => ${file2x}`); | |
exec(`open -a ImageOptim ${file2x}`); | |
}); | |
// @3x | |
im.convert(args(file3x, file3x, 1242), () => { | |
console.log(`${file3x} => ${file3x}`); | |
exec(`open -a ImageOptim ${file3x}`); | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment