Skip to content

Instantly share code, notes, and snippets.

@tkhoa2711
Last active November 17, 2021 03:29
Show Gist options
  • Save tkhoa2711/8fbbaf6c4e8a54bcdd96f8db8d27a4d1 to your computer and use it in GitHub Desktop.
Save tkhoa2711/8fbbaf6c4e8a54bcdd96f8db8d27a4d1 to your computer and use it in GitHub Desktop.
polaroid
const response = event.Records[0].cf.response;
// ...
Sharp(imageBuffer, {
sequentialRead: true,
limitInputPixels: 2684026890, // raise default limit to 10x
})
.resize({
width: width,
withoutEnlargement: true,
})
.toFormat("jpeg", {
mozjpeg: true,
progressive: true,
})
.toBuffer()
.then((processedBuffer) => {
response.status = 200;
response.body = processedBuffer.toString("base64");
response.bodyEncoding = "base64";
response.headers["content-type"] = [
{ key: "Content-Type", value: "image/jpeg" },
];
response.headers["cache-control"] = [
{ key: "Cache-Control", value: "max-age=31536000" }, // 1 year
];
callback(null, response);
})
.catch((err) => {
console.error("Exception while reading source image", err);
callback(null, response);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment