Skip to content

Instantly share code, notes, and snippets.

@whisher
Created September 17, 2018 15:39
Show Gist options
  • Select an option

  • Save whisher/2adf4f51d611cd727a25dbf405de1680 to your computer and use it in GitHub Desktop.

Select an option

Save whisher/2adf4f51d611cd727a25dbf405de1680 to your computer and use it in GitHub Desktop.
exports.gallery = (req, res, next) => {
const imagePath = './images/';
const sizes = [200, 800];
const image = `${imagePath}${req.file.filename}`;
const ext = path.extname(image);
const filename = path.basename(image, ext);
const resize = size => sharp(`${image}`)
.resize(size, size)
.toFile(`${imagePath}${filename}-${size}${ext}`);
Promise
.all(sizes.map(resize))
.then(metadata => {
return metadata;
})
.then(metadata => {
const url = getUrl(req);
const src = url + '/images/' + `${filename}-${sizes[0]}${ext}`;
const data = {src: src, name: filename};
res.status(200).json(data);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment