Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrency issue #27

Open
beniper opened this issue Feb 27, 2019 · 3 comments
Open

Concurrency issue #27

beniper opened this issue Feb 27, 2019 · 3 comments

Comments

@beniper
Copy link

beniper commented Feb 27, 2019

I am currently using the following code to create the png and display image in browser.
The webpage contains several images displayed "on the fly" using text2png using an URL parser
(ex. https://domain/text?s=14&w=20&c=fff&b=315CD5&p=10&t=Lorem%20ipsum%20dolor.)

When I load the page several times however, some images get mixed up and I am wondering why.

Here is the method used to create pngs :
fs.writeFileSync('result.png', text2png(text, option));
res.sendFile(__dirname + '/result.png');

Is there a better method to prevent issues when generating multiple images at the same time?

Thank you,
Benjamin

@warent
Copy link

warent commented May 5, 2019

I think I might be experiencing the same issue

@tkrkt
Copy link
Owner

tkrkt commented May 7, 2019

It is better to response the image directly without writing to the file.

/* Untested code */

// header
res.writeHead(200, {
  'Content-Type': 'image/png',
  'Content-Disposition': 'attachment; filename="result.png"'
});

// 1. using stream
const stream = text2png(text, {
  // ...
  output: 'stream'
});
stream.on('end', () => res.end());
stream.pipe(res);

// 2. using binary
const binary = text2png(text, {
  // ...
  output: 'buffer'
});
res.end(binary, 'binary');

@warent
Copy link

warent commented May 9, 2019

My problem ended up being different from this. It turned out that some of the fonts had the same metadata. So even though the fonts themselves were different, however the internal registerFont works caused the server to mix up the fonts.

The fix was to change the metadata to ensure the fonts were distinct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants