Add API to render a page to a string, encoded in base 64
[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.
[email protected] commented:
Covered by commit: https://github.com/detro/phantomjs/commit/3dc83c606dbf22aaddda4892a7916b7c321190c6
Inside branch: https://github.com/detro/phantomjs/tree/ghostdriver-dev
[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: ---)
[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.
[email protected] commented:
Raw data is possible as a further enhancement. As a start, I think base64 encoded image data should already work.
[email protected] commented:
@trevordixon: we don't have a bridge for ByteArrays yet in QtWebKit. When we do, raw bytes will be added.
[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 :)
[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?
[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 :)
[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.
[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.
[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?
[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...
[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.
[email protected] commented:
Updated in this commit: https://github.com/detro/phantomjs/commit/b715e9a36ebc0b24dadf836bc37947eaf8c2d9cd