Created
February 11, 2018 21:10
-
-
Save srph/c795db13adba3373392c33dbe2cb9342 to your computer and use it in GitHub Desktop.
Node: Scale images to ideal size
This file contains hidden or 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
const Jimp = require('jimp') | |
const path = require('path') | |
const glob = require('./utils/glob') | |
const optim = 300 | |
async function main() { | |
const files = await glob(path.resolve(__dirname, 'imgsrc/*')) | |
files.forEach(async file => { | |
const img = await Jimp.read(file) | |
// We won't have to resize | |
if (img.bitmap.width <= optim) { | |
return | |
} | |
// Reduce to optimal size | |
const scaleDifference = (optim / img.bitmap.width) | |
img.scale(scaleDifference) | |
img.write(path.resolve(__dirname, 'imgdump', path.basename(file))) | |
}) | |
} | |
try { | |
main() | |
} catch(e) { | |
console.log(e) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment