can I use image url as sharp input?
See original GitHub issueI’m just started with Sharp, and trying to overlay two images in the nodejs server and output a png, both are web images. So I’m trying with (picked random img urls here)
const server = http.createServer(async (req, res) => {
function processImg(){
let buffer = sharp('https://pbs.twimg.com/profile_images/3572978953/8c522d3ea384cd42e46a4a2498300c35_400x400.jpeg')
.composite({input: 'https://abs.twimg.com/a/1569889127/img/delight/delight_prompt_3.png'})
.png()
.toBuffer()
.catch(err=>{console.log(err)})
return buffer
}
let newBuffer = await processImg();
res.setHeader("Content-Type", "image/png");
res.end(newBuffer);
}
Then I get error [Error: Input file is missing]
. Does it mean the image src was not working?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top Results From Across the Web
How can you resize an image in NodeJS using Sharp having ...
To get a buffer from a axios response, you'll have to set responseType to 'arraybuffer' . const imageResponse = await axios({url: url, ...
Read more >How To Process Images in Node.js With Sharp - DigitalOcean
Node.js has an ecosystem of libraries you can use to process images, such as sharp, jimp, and gm module. This article will focus...
Read more >Processing images with sharp in Node.js - LogRocket Blog
sharp is a high-performance image processing module for Node.js. Learn how to use sharp for UGC management.
Read more >Process Images the right way | SharpJS - Programmer Couple
A complete guide on Sharp.js, a light and easy Javascript Library used for Image Processing.
Read more >Output options - High performance Node.js image processing
If an explicit output format is not selected, it will be inferred from the extension, with JPEG, PNG, WebP, AVIF, TIFF, GIF, DZI,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
As @lovell mentioned, Sharp does not support remote URL as Input.
Anyway, There’s another way to achieve your goal - Download Image manually & Use Buffer as Input:
using Axios
using Request
Unfortunately. request does not support Promise interface. You’ll have to wrap request calls to your own Promise instance:
Anyway, Based on your code example, It’s not good to production usage because someone calls your server, every time server should do expensive tasks (download image from remote server and transform image - imagine If someone calls your server 100 times concurrently - your server will be unavailable.)
To optimize performance issue, You should consider caching.
If composite image is always same, you can store composite image to memory and re-use them:
Anyway, Upper example can reduce number of downloading composite image. but image composition is still required.
To reduce image composition tasks. You can setup your own CDN in front of your server. See https://github.com/donnemartin/system-design-primer#content-delivery-network for details.
Thank you @lovell , the example helped a lot! Able to resolve the problem.