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

Send images on the fly #8

Closed
slidenerd opened this issue Dec 20, 2016 · 5 comments
Closed

Send images on the fly #8

slidenerd opened this issue Dec 20, 2016 · 5 comments

Comments

@slidenerd
Copy link

slidenerd commented Dec 20, 2016

First of all awesome work on the 100% PURE image processing library. Hats off to that. I have a usecase where I need to generate images containing text and they cant be stored as there are gonna be 100/1000s of them. Is there a way I can generate an image and send it to the client as a url that the client can view. Basically talking about creating an image on the fly

EDIT 1
It seems a base 64 url would do the job. Can I convert a generated image to a base 64 url

Thank you for your help in advance

@omril1
Copy link

omril1 commented Dec 29, 2016

Base 64 would work for images, but it sometimes causes problems with larger ones.
I think what you are looking for is piping streams, and that's the same reason I came here too.

@joshmarinacci
Copy link
Owner

Yes, you can do this. You'll need to store them in memory as a buffer instead of saving them to disk. However, you'll still need to serve them to the browser regardless of how they are stored. Your options are to base 64 encode them as a data URL, or you can render them directly to the data stream, referenced by a unique id. ex:

<img src="http://theserver.com/render?format=png&text=foobar"/>

then your server does something like

in event handler for the /render URL route
  var ctx = img.getContext('2d');
  //draw stuff
  img.renderToStream(res);

@TheLogan
Copy link

Doing a bit of necromancy here.
Like the OP I'd like to send images as a datauri, they are all below 200x200 px bandwith shouldn't be an issue here, but I can't seem to figure out how I would generate the datauri from my img object.
I've tried using https://www.npmjs.com/package/datauri which takes a buffer, but the buffer from img.data fails when I try.
I don't seem to have the 'renderToStream' function either. I do have 'encodePNGToStream', but I can't figure out how to use it for generating a datauri either.
Any help would be appreciated.

@robertmain
Copy link
Contributor

How does it fail? What error does it give?

@TheLogan
Copy link

Well, the output seems to be an order of magnitude larger than what I expect
(https://pastebin.com/BRHzYvvN)
And when I try to use the datastring as an image I'm being told that "the image cannot be displayed because it contains errors", which is not a lot to go on.

What I'm trying to do is:

datauri.format('.png', img.data);
And then use
datauri.content

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

5 participants