Mashup Image Generator

node v18.11.0
version: 1.0.0
endpointsharetweet
This endpoint takes a series of images and creates an animated gif.
const GifEncoder = require("gif-encoder-2"); const Jimp = require("jimp"); const fs = require("fs"); const imageUrlsOld = [ "https://lh3.googleusercontent.com/lHexKRMpw-aoSyB1WdFBff5yfANLReFxHzt1DOj_sg7mS14yARpuvYcUtsyyx-Nkpk6WTcUPFoG53VnLJezYi8hAs0OxNZwlw6Y-dmI=s130", "https://lh3.googleusercontent.com/GwnosrkaneKGEkWySxvTSzZ5bEUjWkRuQzLSNfrpgy2-gxYjoR3m5PohLT9Fzy0p1tohajZ1g-LFfF_ZLnS1GqlPNHPUaKUbDhbf=s130", // Add more URLs as needed ]; const delay = 300; // Time in milliseconds between image switches async function downloadImage(url) { const response = await fetch(url); const arrayBuffer = await response.arrayBuffer(); return Buffer.from(arrayBuffer); } async function createAnimatedGif(imagesUrls) { const imageBuffers = await Promise.all(imageUrls.map(downloadImage)); const images = await Promise.all(imageBuffers.map((buffer) => Jimp.read(buffer))); const width = images[0].bitmap.width; const height = images[0].bitmap.height; const gif = new GifEncoder(width, height); gif.setDelay(delay); gif.setRepeat(0); gif.start(); for (const img of images) { const resizedImage = img.clone().resize(width, height); const rawData = Buffer.from(resizedImage.bitmap.data); gif.addFrame(rawData); } // gif.on("data", (data) => { // fs.appendFileSync("animated.gif", data); // }); // gif.on("end", () => { // console.log("Animated GIF created as animated.gif"); // }); res.setHeader("Content-Type", "image/gif"); gif.on("data", (data) => { res.write(data); }); gif.on("end", () => { res.end(); }); gif.finish(); } exports.endpoint = async function(request, response) { const data = request.url.split('?images=')[1]; const imageBuffers = await Promise.all(imageUrls.map(downloadImage)); const images = await Promise.all(imageBuffers.map((buffer) => Jimp.read(buffer))); const width = images[0].bitmap.width; const height = images[0].bitmap.height; const gif = new GifEncoder(width, height); gif.setDelay(delay); gif.setRepeat(0); gif.start(); for (const img of images) { const resizedImage = img.clone().resize(width, height); const rawData = Buffer.from(resizedImage.bitmap.data); gif.addFrame(rawData); } response.setHeader("Content-Type", "image/gif"); gif.on("data", (data) => { response.write(data); }); gif.on("end", () => { response.end(); }); gif.finish(); } // https://untitled-rog15kqs9wix.runkit.sh/?images=https://lh3.googleusercontent.com/lHexKRMpw-aoSyB1WdFBff5yfANLReFxHzt1DOj_sg7mS14yARpuvYcUtsyyx-Nkpk6WTcUPFoG53VnLJezYi8hAs0OxNZwlw6Y-dmI=s130,https://lh3.googleusercontent.com/GwnosrkaneKGEkWySxvTSzZ5bEUjWkRuQzLSNfrpgy2-gxYjoR3m5PohLT9Fzy0p1tohajZ1g-LFfF_ZLnS1GqlPNHPUaKUbDhbf=s130
Loading…

no comments

    sign in to comment