Last active
March 13, 2021 13:38
-
-
Save jonath92/d8ebaf1051a30b758ccbdb935570949a to your computer and use it in GitHub Desktop.
Generate Thumbnail of Image and save it to file
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
/** | |
* simple function for generating an Image Thumbnail and save it to a file | |
* Usage: | |
* 1. copy script and original image in a folder | |
* 2. run 'npm init --y && npm i image-thumbnail fs' | |
* 3. execute script with node | |
*/ | |
const imageThumbnail = require('image-thumbnail'); | |
const { promises: fs } = require("fs"); | |
async function generateThumb() { | |
try { | |
// default is 10 but I think 5 is sufficient | |
const thumbnail = await imageThumbnail('./original.jpg', { percentage: 5 }); | |
await fs.writeFile('thumbnail.jpg', thumbnail, { encoding: 'base64' }) | |
} catch (err) { | |
console.error(err); | |
} | |
} | |
generateThumb() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment