Skip to content

Instantly share code, notes, and snippets.

@srph
Created February 11, 2018 21:10
Show Gist options
  • Save srph/c795db13adba3373392c33dbe2cb9342 to your computer and use it in GitHub Desktop.
Save srph/c795db13adba3373392c33dbe2cb9342 to your computer and use it in GitHub Desktop.
Node: Scale images to ideal size
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