Skip to content

Instantly share code, notes, and snippets.

@lindesvard
Created March 26, 2018 13:16
Show Gist options
  • Save lindesvard/8fb1034ab7ff41f58828374d57de7ab4 to your computer and use it in GitHub Desktop.
Save lindesvard/8fb1034ab7ff41f58828374d57de7ab4 to your computer and use it in GitHub Desktop.
Resize image with Jimp
var Jimp = require("jimp");
var url =
"http://cdn.wonderfulengineering.com/wp-content/uploads/2016/01/Desktop-Wallpaper-2.jpg";
const saveAndResize = (image, name, width, height, extension) => {
image
.clone()
.cover(width, height)
.quality(50)
.write(
`${name}-${Math.ceil(width)}x${Math.ceil(height)}.${extension ||
image.getExtension()}`
);
};
Jimp.read(url)
.then(image => {
const name = "image";
saveAndResize(image, name, 700, 393.75, "jpg");
saveAndResize(image, name, 200, 200, "jpg");
saveAndResize(image, name, 100, 100, "jpg");
})
.catch(function(err) {
console.error(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment