脚本宝典收集整理的这篇文章主要介绍了php – 如何将html&css转换为图像?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我最好寻找服务器端解决方案,因为从客户端发布图像可能需要一些时间.
我发现这个:https://code.google.com/p/java-html2image/但不幸的是我只能使用PHP,ruby或客户端技术.
在客户端,您可以使用类似库(使用html 5)的东西:http://html2canvas.hertzen.com/ =)
有了它,你可以使用类似的东西:
html2canvas(document.getElementById("element-id"),{ onrendered: function(canvas) { VAR image = new Image(); image.src = canvas.toDataURL("image/png"); // This should be image/png as browsers (only) support IT (to Biggest compatibilty) // ApPEnd image (yes,it is a DOM element!) to the DOM and etc here.. } });
服务器端解决方案
要获得服务器端解决方案,可以使用PhantomJS(使用webkit)或SlimerJS(使用Gecko).
一个好的库是这两个的包装器是CasperJS.使用CasperJS,可以使用的代码是:
casper.start(casper.cli.args[0],function() { this.captureSelector(casper.cli.args[1],casper.cli.args[2]); }); casper.run();
将它保存为screenshot.js(只是名称的一个示例,您也可以选择一个名称)并使用以下内容运行它:
casperjs screenshot.js (URL) (image path) (selector)
从任何语言.
服务器端的(可能更好)替代方案
其他选项是使用Selenium,但这仅在您可以在服务器中运行Java(并手动安装浏览器)时才有效(PhantomJS / SlimerJS / CasperJS,但是没有这些要求)
仅在您需要完全模拟浏览器时使用它(可能在使用插件时…)
最好的Selenium是你可以使用包装器连接到它(使用Selenium Server),请参阅文档以获取列表:http://code.google.com/p/selenium/w/list
以上是脚本宝典为你收集整理的php – 如何将html&css转换为图像?全部内容,希望文章能够帮你解决php – 如何将html&css转换为图像?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。