phantomjs icon indicating copy to clipboard operation
phantomjs copied to clipboard

Add API to render a page to a string, encoded in base 64

Open detro opened this issue 14 years ago • 29 comments

[email protected] commented:

A good start would be support for:

  • PNG
  • JPG
  • BMP

The API could be something like:

var p = require("webpage").create();

p.open("http://www.repubblica.it", function(status) { if (status === "success") { require("fs").write("img", "data:image/png;base64,"+p.renderBase64PNG(), "w"); }

});

This is important for the PhantomJS WebDriver: GhostDriver.

Disclaimer: This issue was migrated on 2013-03-15 from the project's former issue tracker on Google Code, Issue #547. :star2:   10 people had starred this issue at the time of migration.

detro avatar May 17 '12 03:05 detro

[email protected] commented:

Covered by commit: https://github.com/detro/phantomjs/commit/3dc83c606dbf22aaddda4892a7916b7c321190c6

Inside branch: https://github.com/detro/phantomjs/tree/ghostdriver-dev

detro avatar May 17 '12 03:05 detro

[email protected] commented:

Since this is very similar to issue 15, I suggest we merge them.

I think I like the idea of a function returning the rendering content (as image), as opposed to a special magic URL.

 
Metadata Updates

  • Label(s) removed:
    • Type-Defect
    • Priority-Low
  • Label(s) added:
    • Type-Enhancement
    • Priority-Medium
    • Component-Logic
    • Module-WebPage
  • Milestone updated: Release1.6 (was: ---)

ariya avatar May 21 '12 05:05 ariya

[email protected] commented:

I think this is a different approach to #15 though: but if We add an api to read the image as raw bytes, and the ability to pass those raw bytes to a webserver response object, than 15 can be fulfilled.

detro avatar May 21 '12 06:05 detro

[email protected] commented:

Raw data is possible as a further enhancement. As a start, I think base64 encoded image data should already work.

ariya avatar May 21 '12 11:05 ariya

[email protected] commented:

I would prefer raw bytes. You could always btoa() it for base64.

trevordixon avatar May 31 '12 09:05 trevordixon

[email protected] commented:

@trevordixon: we don't have a bridge for ByteArrays yet in QtWebKit. When we do, raw bytes will be added.

detro avatar Jun 01 '12 23:06 detro

[email protected] commented:

 

 
Metadata Updates

  • Label(s) added:
    • webdriver

detro avatar Jun 12 '12 06:06 detro

[email protected] commented:

Landed in https://github.com/ariya/phantomjs/commit/51ab11c6d9.

ariya avatar Jun 15 '12 13:06 ariya

[email protected] commented:

Issue 15 has been merged into this issue.

ariya avatar Jun 15 '12 13:06 ariya

[email protected] commented:

Do we really need to render into JPG and BMP format at all?

ariya avatar Jun 15 '12 14:06 ariya

[email protected] commented:

I added it out of "completeness": it was few extra lines, and the code was all there. You never know: some people even asked for unencoded bytes, remember? People have weird tastes :)

detro avatar Jun 15 '12 14:06 detro

[email protected] commented:

Regardless file format, I have possible suggestion for the API improvement. We can draw a similarity to 2-D Canvas, i.e. its getImageData function. Right now we still don't support binary data (and thus also no support for Canvas-compatible ImageData) but we need to keep that option open for the near future.

Making it similar to Canvas API, it could be as WebPage#getImageBase64. See also http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#pixel-manipulation.

Example:

var result = page.getImageBase64(0, 0, 600, 400);

We could make the arguments optional, e.g. getImageBase64() returns the entire rendered page.

Comments?

ariya avatar Jun 15 '12 14:06 ariya

[email protected] commented:

Wait, you are now talking about method signature (I don't have a preference - fine to use something else) and clipping (we could definitely add that as overloaded method).

The spirit of the getImageBase64 seems very tailored towards the Canvas manipulation though: a matrix of pixels to manipulate. But given the pixel-by-pixel nature, it's a BMP or something even more raw.

Or, I'm completely missing your point :)

detro avatar Jun 15 '12 14:06 detro

[email protected] commented:

In any case, those API are more "basic": get an image encoded in Base64, into a string. Nothing like canvas.

We could add those too, but I'd leave those "basic" API.

Also, those are needed to implement the WebDriver specs fully: once we have a more "canvassy" API, we could deprecate those and provide utility methods to achieve the same result, but passing through the canvassy stuff.

detro avatar Jun 15 '12 14:06 detro

[email protected] commented:

That's a good point, the canvas-like API makes more sense if the actual getImageData() exists (and thus the users can draw the analog). Otherwise, it's kinda confusing.

ariya avatar Jun 17 '12 01:06 ariya

[email protected] commented:

Also, after playing with it for a while, and assuming most people wants PNG (lossless and still good compression) so it can be the default, what about having only (already private) this function

renderBase64(format = 'PNG');

as opposed to 3 different ones. What do you think?

ariya avatar Jun 17 '12 01:06 ariya

[email protected] commented:

It was a dilemma for me: use 1 method and pass a parameter to choose the format, or make the format part of the signature?

I ended up choosing the signature as it was just 1 parameter and JS doesn't have enums (in the C++ way).

We can change it: my personal preference is with the way it is now, but we could even add a 4th "renderBase64(format = "png")" as you suggest. But this makes it 4 methods...

detro avatar Jun 17 '12 12:06 detro

[email protected] commented:

That was my line of thought as well. However, I make an assumption here that most people will want PNG 99% of the time, hence it'll be only 'renderBase64()' we are going to see in many places.

ariya avatar Jun 17 '12 12:06 ariya

[email protected] commented:

Updated in this commit: https://github.com/detro/phantomjs/commit/b715e9a36ebc0b24dadf836bc37947eaf8c2d9cd

detro avatar Jun 18 '12 08:06 detro

[email protected] commented:

 

 
Metadata Updates

  • Status updated: Fixed

ariya avatar Jun 19 '12 12:06 ariya