Skip to content

Instantly share code, notes, and snippets.

@acorcutt
Created February 27, 2022 21:47
Show Gist options
  • Save acorcutt/1fc4fcf2d110965604043e0edf11bef7 to your computer and use it in GitHub Desktop.
Save acorcutt/1fc4fcf2d110965604043e0edf11bef7 to your computer and use it in GitHub Desktop.
Testing sharp image merge
import sharp from 'sharp';
export default async function handler(req, res) {
const { url, logo } = req.query || [];
const imgRes = await fetch(url);
const logoRes = await fetch(logo);
//return res.status(200).json({ url, status: imgRes.status });
const imgBuffer = Buffer.from(await imgRes.arrayBuffer());
const logoBuffer = Buffer.from(await logoRes.arrayBuffer());
const logoPipeline = sharp(logoBuffer);
logoPipeline.resize({ width: 100, height: 100 });
const imgPipeline = sharp(imgBuffer);
imgPipeline.resize({ width: 800 });
imgPipeline.composite([{ input: await logoPipeline.toBuffer(), gravity: 'center' }]);
imgPipeline.jpeg({ quality: 70 });
const outBuffer = await imgPipeline.toBuffer();
return res.end(outBuffer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment