phantomjs output file size: png v gif

半城伤御伤魂 提交于 2019-12-13 02:01:58

问题


With phantomjs you can choose the file format to use for page.render().

I'm finding that the file size I'm getting for png is around three times higher than what I'm getting for gif. I wasn't aware that png should be any worse (in terms of file size) than gif; in fact I thought png was meant to be better.

Unfortunately, I kinda need to output to png because of its support for variable opacity, but the larger file size is a bit of an issue.

So, is there any way in which I can control file size of the png? Maybe change the encoding scheme or something? I'm currently using phantomjs 1.9.8.


回答1:


Inside of PhantomJS

No, there is no way to make the png file size smaller, but there is a way to make it bigger (just for fun):

  1. Render the file to png,
  2. load the file to a canvas of appropriate size,
  3. get the Data-URI of the canvas in png or any other format,
  4. decode the Base 64 part and write to file (this is very tricky to get right).

PhantomJS 1.x has a bug which results in a vastly inflated, but valid file.

Only jpeg rendering enables you to specify a quality setting which will result in a smaller file size, but then again jpeg doesn't support transparency.

You could also see whether PhantomJS 2.0.0 behaves better, because it has an engine underneath it that is almost three years newer than in PhantomJS 1.x.

Outside of PhantomJS

Your best bet would be to render the png in PhantomJS as-is and post-process it with your favorite library. It may even be enough to open it and save it again.

You can for example call an installed program with the child process module or your can open a webpage that contains such a service and upload the captured file or base64 representation of it. The possibilities are endless.



来源:https://stackoverflow.com/questions/33017497/phantomjs-output-file-size-png-v-gif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!