Created
March 26, 2018 13:16
-
-
Save lindesvard/8fb1034ab7ff41f58828374d57de7ab4 to your computer and use it in GitHub Desktop.
Resize image with Jimp
This file contains hidden or 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
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