Created
February 14, 2024 13:49
-
-
Save Wang-Cankun/0e0b9667a1b7eb49ab3614e3b732d400 to your computer and use it in GitHub Desktop.
Node.js Script for Resizing and Converting PNG Images
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
// npm install jimp | |
const Jimp = require('jimp') | |
const outputSizes = [ | |
{ name: 'android-chrome-192x192.png', width: 192, height: 192 }, | |
{ name: 'android-chrome-512x512.png', width: 512, height: 512 }, | |
{ name: 'apple-touch-icon.png', width: 180, height: 180 }, | |
{ name: 'favicon-16x16.png', width: 16, height: 16 }, | |
{ name: 'favicon-32x32.png', width: 32, height: 32 }, | |
{ name: 'favicon.ico', width: 32, height: 32 }, | |
] | |
async function resizeImage(inputFile) { | |
try { | |
const image = await Jimp.read(inputFile) | |
const resizePromises = outputSizes.map((size) => { | |
return image | |
.clone() | |
.resize(size.width, size.height) | |
.writeAsync(`./output/${size.name}`) | |
}) | |
await Promise.all(resizePromises) | |
console.log('All images have been resized successfully.') | |
} catch (err) { | |
console.error('Error resizing images:', err) | |
} | |
} | |
resizeImage('biocypher_logo_raw.png') | |
// Run: | |
// node convert.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to run:
mkdir image-resizer
. The raw png imagebiocypher_logo_raw.png
should put into this foldernpm install jimp
.convert.js
in your project directory. Paste the provided JavaScript code into convert.js.node convert.js
. The resized images should be in theoutput
folder