-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2afb18e
commit 6e10823
Showing
4 changed files
with
479 additions
and
3,527 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
const Jimp = require('jimp'); | ||
const TinyColor = require('tinycolor2'); | ||
const sharp = require('sharp'); | ||
|
||
module.exports = async ({ background, type = 'resize', width, height }, buffer) => { | ||
type = type === 'resize' ? 'cover' : type; | ||
// if only one of width/height was provided, make it a square or let Jimp decide if it is able | ||
if (width && !height) { | ||
height = type === 'resize' ? Jimp.AUTO : width; | ||
const resizeOptions = { fit: type }; | ||
if (width) { | ||
resizeOptions.width = width; | ||
} | ||
if (height && !width) { | ||
width = type === 'resize' ? Jimp.AUTO : height; | ||
if (height) { | ||
resizeOptions.height = height; | ||
} | ||
const jimpImage = await Jimp.read(buffer); | ||
jimpImage[type](width, height); | ||
if (background) { | ||
const color = new TinyColor(background); | ||
jimpImage.background(parseInt(color.toHex8(), 16)); | ||
resizeOptions.background = background; | ||
} | ||
try { | ||
return sharp(buffer) | ||
.resize(resizeOptions) | ||
.toBuffer(); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
return jimpImage.getBufferAsync(Jimp.AUTO); | ||
}; |
Oops, something went wrong.