Skip to content

Instantly share code, notes, and snippets.

@tsh-code
Created October 2, 2018 11:38
Show Gist options
  • Save tsh-code/21a22b8e0d7cbc31d3c7a93711ef5209 to your computer and use it in GitHub Desktop.
Save tsh-code/21a22b8e0d7cbc31d3c7a93711ef5209 to your computer and use it in GitHub Desktop.
router.post('/:id/title-image', (req, res, next) => {
lwip.open(req.file.buffer, 'jpg', (err, image) => {
if (err) { next(err); return; }
let ratio = (image.width() > 960 ? (960 / image.width()) : 1);
image.scale(ratio, (err, image) => {
if (err) { next(err); return; }
image.crop(image.width(), Math.min((image.width() / 2), image.height()), (err, image) => {
if (err) { next(err); return; }
image.toBuffer('jpg', { quality: 80 }, (err, buffer) => {
if (err) { next(err); return; }
db.doc.attachment.write(req.params['id'], "TITLE_IMAGE", buffer, "image/jpeg", (err) => {
if (err) { next(err); return; }
res.sendStatus(200);
});
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment