Skip to content

Instantly share code, notes, and snippets.

@sobstel
Created October 6, 2017 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sobstel/19ac8218d6c5b9bc115020d906d2bc5f to your computer and use it in GitHub Desktop.
Save sobstel/19ac8218d6c5b9bc115020d906d2bc5f to your computer and use it in GitHub Desktop.
Optimal images optimization
//
// 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