Skip to content

Instantly share code, notes, and snippets.

@jonath92
Last active March 13, 2021 13:38
Show Gist options
  • Save jonath92/d8ebaf1051a30b758ccbdb935570949a to your computer and use it in GitHub Desktop.
Save jonath92/d8ebaf1051a30b758ccbdb935570949a to your computer and use it in GitHub Desktop.
Generate Thumbnail of Image and save it to file
/**
* 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