question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Composing multiple images generated by Sharp

See original GitHub issue

Below is my code that will generate images of cards. It’s a snippet of a much larger set of code, but only this section is relevant.

It runs up until “Overlaying suit icon…” that’s the last output in the console. After that, “Overlaying card icon…” does not display.

The only way to make it run is to remove .toBuffer from the first promise callback (after Overlaying suit icon).

return sharp(buff).overlayWith(suitIcon, { left: config.get('template.cards.suit-icon.left'), top: config.get('template.cards.suit-icon.top') }).toBuffer();

Becomes

return sharp(buff).overlayWith(suitIcon, { left: config.get('template.cards.suit-icon.left'), top: config.get('template.cards.suit-icon.top') });

And then needless to say the code after that fails to complete. I’ve seen many solutions to overlaying multiple images on top of each other on here, many of them years old and actually I struggle to get any of the examples to work.

With promises / etc I am not expert, so any help is appreciated. Please refrain from tearing my code apart I know it is probably horrible.

var cardImage = sharp(cardPath + 'card-background.svg')
				.png()
				.toBuffer()
				.then(function(buff){
					// Red
					if( ['heart', 'diamond'].indexOf(cardCombo[card][1]) >= 0 ){
						var suitIcon = cardPath + 'icon-'+cardCombo[card][1]+'.svg';
						var cardIcon = cardPath + 'icn-'+cardCombo[card][0]+'-red.svg';
					}
					// Black
					else {
						var suitIcon = cardPath + 'icon-'+cardCombo[card][1]+'.svg';
						var cardIcon = cardPath + 'icn-'+cardCombo[card][0]+'-black.svg';
					}

					console.log('Overlaying suit icon...');
					return sharp(buff).overlayWith(suitIcon, {
								left: config.get('template.cards.suit-icon.left'),
								top: config.get('template.cards.suit-icon.top')
							}).toBuffer();

					
				}).then(function(buffTwo){
					console.log('Overlaying card icon...');
					console.log(buffTwo.toBuffer());
					return sharp(buffTwo, opts).overlayWith(cardIcon, {
						left: config.get('template.cards.card-icon.left'),
						top: config.get('template.cards.card-icon.top')
					}).png().toFile(finalFilePath + '.png');
				}).then(function(res){
					console.log(res);
				}).catch(err => {
					console.log(err);
				})

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Brandoningcommented, Jun 6, 2018

Andddd apologies but this seems it was from my lack of Node understanding! Or a quirk in the library, not too sure at this point.

Just in case anyone else gets stuck in a similar headache, if you’re running a large amount of operations in nested promises (.toBuffers()) it seems it wants to finish all of level 1, before it triggers any promises of level 2.

E.g this section of code (green) will finish for all the items in my loop before the second section (red) starts at all, then all items are looped through as intended from what I can tell.

screen shot 2018-06-06 at 10 16 35

0reactions
xianghu1314commented, Dec 14, 2018

you can do it by await/async like this: img=await img.overlayWith(uname, { left:320, top:360 }).toBuffer(); img=sharp(img); img = await img.overlayWith(cardid, { left:320, top:405 }).toBuffer(); img=sharp(img);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compositing images - sharp - Sharp
Resize large images in common formats to smaller, web-friendly JPEG, PNG, WebP, GIF and AVIF images of varying dimensions.
Read more >
How To Process Images in Node.js With Sharp - DigitalOcean
Open your terminal and create the directory for the project using the mkdir command: mkdir process_images.
Read more >
Processing images with sharp in Node.js - LogRocket Blog
The sharp module's composite() method takes an array as an input. The image to be composited, robo.jpg , is read by a single...
Read more >
In Nodejs sharp package how to add two image into a single ...
I tried to combine two images into a single image using sharp package in NodeJS but i cant is there any ways to...
Read more >
Enhancement: support multiple overlayWith in single pipeline ...
I'm looking for option to combine multiple images, side by side or one under another. I looked the overlayWith function but seems it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found