Reputation: 1789
I am tring to get image from url and then to resize on fly without saving
var request = require('request');
request
.get('s3url')
.on('response', function (response) {
console.log(response) // 200
console.log(response.headers['content-type']) // 'image/png'
})
.pipe(res)
I can now return same picture with request
lib but how can I manipulate it and then return as response?
Upvotes: 2
Views: 5237
Reputation: 1282
Full example with Sharp. check the github link: https://github.com/surojitp/imageResizeOnTheFly
import express from 'express';
const app = express();
import sharp from "sharp";
import got from "got";
app.get('/',(req, res, next) =>{
let image = req.query.src ? req.query.src : 'https://thumbs.dreamstime.com/b/businessman-hand-writing-demo-marker-business-concep-concept-96434578.jpg';
let width = parseInt(req.query.width ? req.query.width : 100);
let height = parseInt(req.query.height ? req.query.height : 100);
var transformer = sharp()
.resize(width, height)
.on('info', function(info) {
// console.log('Image height is ' + info.height);
});
/********* Using GOT */
got.stream(image).pipe(transformer).pipe(res);
});
app.listen(8080);
Example: http://localhost:8080/?src=https://buket.s3.amazonaws.com/images/defaultimg.png&&height=200&&width=200
Upvotes: 0
Reputation: 4716
There are several npm modules that do resizing one example is sharp
. You can use it in this way (example taken from the API documentation).
var transformer = sharp()
.resize(300)
.on('info', function(info) {
console.log('Image height is ' + info.height);
});
readableStream.pipe(transformer).pipe(res);
the readableStream
would be the stream of your original picture.
Upvotes: 7