Created
January 21, 2020 20:49
-
-
Save TehShrike/451d3b6531fee2ea573eddc8a56a58bf to your computer and use it in GitHub Desktop.
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 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