Sunday 22 October 2017

[Question]Rescaling/resizing images that doesn't live on my server on the fly

I'm writing a messenger chatbot that would sometimes serve images that doesn't live on my server (from url)facebook generic template stays that the picture should be in 1.91:1 ratioUse the correct aspect ratio for your image. Photos in the generic template that aren't 1.91:1 will be scaled or cropped.one way to do this was to expose /image endpoint that would take three parameters url,width,height and resize the image to the specified dimensions by w ,hsomething like :app.get ('/image', function (req, res, next) { let url = decodeURI (req.query.url); Jimp.read (url, function (err, img) { if (err) throw err; img .resize (~~req.query.w, ~~req.query.h) .getBuffer (Jimp.AUTO, function (e, buffer) { if (e) throw e; res.writeHead (200, { 'Content-Type': 'image/jpeg', }); res.end (buffer); }); }); }); and then when i need to serve a picture I'd redirect to this endpoint to rescale the image as i wishHowever this approach doesn't seems to work not even on my dev because :1-facebook would ask for like 9 pictures at time2-since node is single threaded it will process the requests in order3-resizing an image seems to be CPU intensive and it does block the event loop4-when the requests times out facebook retries to send them and block the event loop even for longer time

Submitted October 22, 2017 at 12:33PM by 10701220

No comments:

Post a Comment