Skip to content

Instantly share code, notes, and snippets.

@TehShrike
Created January 21, 2020 20:49
Show Gist options
  • Save TehShrike/451d3b6531fee2ea573eddc8a56a58bf to your computer and use it in GitHub Desktop.
Save TehShrike/451d3b6531fee2ea573eddc8a56a58bf to your computer and use it in GitHub Desktop.
const Jimp = require(`jimp`)
const imagemin = require('imagemin')
const imageminPngquant = require('imagemin-pngquant')
const { readdirSync } = require(`fs`)
const { join } = require(`path`)
const relativeToThisFile = relativePath => join(__dirname, relativePath)
const inputPath = relativeToThisFile(`../../book-cover-image`)
const outputPath = relativeToThisFile(`../../Markdown/Web/book-image/thumbnail`)
const files = readdirSync(inputPath)
// 900x1350
const toTransform = files.filter(file => /\.png$/.test(file))
toTransform.forEach(filename => {
const path = join(inputPath, filename)
Jimp.read(path).then(image => {
console.log(`Writing`, filename)
return new Promise((resolve, reject) => {
const filePath = join(outputPath, filename)
image
.autocrop()
.resize(400, 600)
.crop(0, 0, 400, 400)
.write(filePath, err => err ? reject(err) : resolve(filePath))
})
}).then(filePath => {
imagemin([filePath], outputPath, {
plugins: [
imageminPngquant({ quality: '70-80' })
]
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment