在没有shell的情况下将HTML转换为图像 [英] Convert HTML to Image in PHP without shell

查看:115
本文介绍了在没有shell的情况下将HTML转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要将HTML转换为图像并将结果显示给用户。我将使用PHP创建一个$ html变量,而不是使用echo $ html显示,我想将其显示为图像,以便用户可以在需要时保存文件。



我希望能有像$ image = convertHTML2Image($ html)这样简单的东西; :p如果存在?!



感谢!!

解决方案

正如@Pekka所说,将HTML代码转换为图像的工作是成熟的Web浏览器的工作。



如果你想做这种事情,因此您需要有一个执行以下操作的脚本:
$ b


  1. 在浏览器中打开该页面。

  2. 从浏览器捕获呈现的页面作为图形。

  3. 将图形输出到用户。



传统上,这将是一项艰巨的任务,因为网络浏览器通常由用户驱动,并不容易以这种方式自动化。



幸运的是,现在是解决方案,形式为 PhantomJS



PhantomJS是一个无头浏览器,专为这种事情设计 - 自动化任务需要一个完整的渲染引擎。



它基本上是一个完整的兄弟wser,但没有用户界面。它将页面内容完全呈现为另一个浏览器(它基于Webkit,因此结果与Chrome相似),并且可以通过脚本进行控制。



因为它在PhantomJS主页上说,其目标用例之一是用于截取网站的截图或缩略图。

(另一个好用的是自动测试您的网站,它也是一个很好的工具)



希望有帮助。


I want the option of converting HTML to image and showing the result to the user. I would be creating an $html variable with PHP, and instead of displaying using echo $html, I want to display it as an image so the user can save the file if they needed to.

I was hoping there would something as simple as $image = convertHTML2Image($html); :p if that exists?!

Thanks!!

解决方案

As @Pekka says, the job of turning HTML code into an image is the job of a full-blown web browser.

If you want to do this sort of thing, you therefore need to have a script that does the following:

  1. Opens the page in a browser.
  2. Captures the rendered page from the browser as a graphic.
  3. Outputs that graphic to your user.

Traditionally, this would have been a tough task, because web browsers are typically driven by the user and not easy to automate in this way.

Fortunately, there is now a solution, in the form of PhantomJS.

PhantomJS is a headless browser, designed for exactly this kind of thing -- automated tasks that require a full-blown rendering engine.

It's basically a full browser, but without the user interface. It renders the page content exactly as another browser would (it's based on Webkit, so results are similar to Chrome), and it can be controlled by a script.

As it says on the PhantomJS homepage, one of its target use-cases is for taking screenshots or thumbnail images of websites.

(another good use for it is automated testing of your site, where it is also a great tool)

Hope that helps.

这篇关于在没有shell的情况下将HTML转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆