Last active
November 24, 2023 14:45
-
-
Save juji/fb9c8cc76b05bc5e155d95e317216637 to your computer and use it in GitHub Desktop.
Resize images using sharp, convert them to webp
This file contains 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 fs = require('fs/promises') | |
const sharp = require('sharp') | |
// this will create 2 images | |
// one with 1200 width and one with 600 width | |
// it will place them in the current directory | |
// note that the original image(s) will be expected to be png | |
async function start( dirname ){ | |
const images = await fs.readdir(dirname) | |
const pngs = images.filter(v => v.match(/\.png/)) | |
await pngs.reduce((a,b) => { | |
// @ts-ignore | |
a = a.then(async v => { | |
console.log(b) | |
return Promise.all([ | |
sharp(dirname + '/' + b) | |
.resize({ | |
width: 1200 | |
}) | |
.webp({ | |
quality: 80, | |
nearLossless: true, | |
smartSubsample: true | |
}) | |
.toFile( | |
__dirname + '/' + b.replace(/\.png/,'.webp') | |
), | |
sharp(dirname + '/' + b) | |
.resize({ | |
width: 600 | |
}) | |
.webp({ | |
quality: 80, | |
nearLossless: true, | |
smartSubsample: true | |
}) | |
.toFile( | |
__dirname + '/' + b.replace(/\.png/,'-600.webp') | |
), | |
]) | |
}) | |
return a | |
},Promise.resolve(1)) | |
} | |
const dirname = '../somewhere' | |
start( dirname ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment