alexscheelmeyer / node-phantom

bridge to PhantomJS from Node.js
317 stars 117 forks source link

specifying quality factor for jpg images #77

Open fjhub opened 10 years ago

fjhub commented 10 years ago

I came across the ability to specify the jpeg quality factor when reading about PhantomJS and was wondering if node-phantom would be able to handle this capability. So I looked at the call to 'render' and noticed the only parameter to pageRender is the filename. Not knowing how much of this works, specifically to which function 'pageRender' really maps, I put in a test to see if I could add an additional argument for the quality value. Unfortunately I didn't see any variation in the resulting jpeg file. For what its worth, I'm using jsgui-node-render-svg which has the node-phantom dependency.

Is there any way of getting at the quality factor?

thanks


As I was cleaning up things from my test, I realized that bridge.js has the actual call to page.render() so I did another test to check for an additional argument and if present pass it along to page.render() and I got what I expected.

So I guess all this means is that it is currently not supported in this release…I can make my modifications available here if anyone so desires.

markstos commented 10 years ago

I'd be interested in a patch for this, thanks.

fjhub commented 10 years ago

There were 2 modifications I made to support a quality factor for jpeg images.

First, in node-phantom.js, I modified the render function in the case pageCreated' block from:

render:function(filename,callback){ request((socket,[id,'pageRender',filename],callbackOrDummy(callback)); },

to be:

render:function(filename,options,callback){ if(callback === undefined) { request(socket,[id,'pageRender',filename],callbackOrDummy(options)); } else { request(socket,[id,'pageRender',filename,options],callbackOrDummy(callback)); } },

Second, in bridge.js, I modified the case 'pageRender' block from:

case 'pageRender':
    page.render(request[3]);
    respond([id,cmdId,'pageRendered']);
    break;

to be:

case 'pageRender':
   if(request[4]!=undefined) {
       page.render(request[3], request[4]);
   } else {
       page.render(request[3]);
   }
   respond([id,cmdId,'pageRendered']);
   break;

and finally, in my call to page.render, I added the quality factor (which only makes sense with jpg images):

... page.render(imagePath, { quality: qualityFactor }, function (errRender) { ... });