在没有shell的情况下在PHP中将HTML转换为Image

时间:2022-10-20 08:56:20

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.

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

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

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

Thanks!!

谢谢!!

5 个解决方案

#1


12  

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

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

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. 在浏览器中打开页面。
  3. Captures the rendered page from the browser as a graphic.
  4. 从浏览器捕获呈现的页面作为图形。
  5. Outputs that graphic to your user.
  6. 输出图形给您的用户。

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.

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

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

幸运的是,现在有了PhantomJS形式的解决方案。

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

PhantomJS是一款无头浏览器,专为此类设计而设计 - 需要完整渲染引擎的自动化任务。

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.

它基本上是一个完整的浏览器,但没有用户界面。它会像其他浏览器一样呈现页面内容(它基于Webkit,因此结果类似于Chrome),并且可以通过脚本进行控制。

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

正如它在PhantomJS主页上所说,它的一个目标用例是截取网站的截图或缩略图。

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

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

Hope that helps.

希望有所帮助。

#2


3  

This is not possible in pure PHP.

这在纯PHP中是不可能的。

What you call "converting" is in fact a huge, non-trivial task: the HTML page has to be rendered. To do this in PHP, you'd have to rewrite an entire web browser.

你所谓的“转换”实际上是一个巨大的,非平凡的任务:必须呈现HTML页面。要在PHP中执行此操作,您必须重写整个Web浏览器。

You'll either have to use an external tool (which usually taps into a browser's rendering engine) or a web service (which does the same).

您将不得不使用外部工具(通常使用浏览器的渲染引擎)或Web服务(执行相同操作)。

#3


0  

You may have a look at dompdf which is a php framework to convert a html file to a pdf.

你可以看看dompdf这是一个将html文件转换为pdf的php框架。

#4


0  

use WKHTMLTOPDF. works like a charm. it converts to any page to PDF .. a jpeg can be obtained by performing later operation.

使用WKHTMLTOPDF。奇迹般有效。它将任何页面转换为PDF ..通过执行以后的操作可以获得jpeg。

http://code.google.com/p/wkhtmltopdf/

http://code.google.com/p/wkhtmltopdf/

#5


0  

is it possible to convert html to image. However, first you must convert to PDF. see link

是否可以将html转换为图像。但是,首先您必须转换为PDF。看到链接

#1


12  

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

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

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. 在浏览器中打开页面。
  3. Captures the rendered page from the browser as a graphic.
  4. 从浏览器捕获呈现的页面作为图形。
  5. Outputs that graphic to your user.
  6. 输出图形给您的用户。

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.

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

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

幸运的是,现在有了PhantomJS形式的解决方案。

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

PhantomJS是一款无头浏览器,专为此类设计而设计 - 需要完整渲染引擎的自动化任务。

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.

它基本上是一个完整的浏览器,但没有用户界面。它会像其他浏览器一样呈现页面内容(它基于Webkit,因此结果类似于Chrome),并且可以通过脚本进行控制。

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

正如它在PhantomJS主页上所说,它的一个目标用例是截取网站的截图或缩略图。

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

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

Hope that helps.

希望有所帮助。

#2


3  

This is not possible in pure PHP.

这在纯PHP中是不可能的。

What you call "converting" is in fact a huge, non-trivial task: the HTML page has to be rendered. To do this in PHP, you'd have to rewrite an entire web browser.

你所谓的“转换”实际上是一个巨大的,非平凡的任务:必须呈现HTML页面。要在PHP中执行此操作,您必须重写整个Web浏览器。

You'll either have to use an external tool (which usually taps into a browser's rendering engine) or a web service (which does the same).

您将不得不使用外部工具(通常使用浏览器的渲染引擎)或Web服务(执行相同操作)。

#3


0  

You may have a look at dompdf which is a php framework to convert a html file to a pdf.

你可以看看dompdf这是一个将html文件转换为pdf的php框架。

#4


0  

use WKHTMLTOPDF. works like a charm. it converts to any page to PDF .. a jpeg can be obtained by performing later operation.

使用WKHTMLTOPDF。奇迹般有效。它将任何页面转换为PDF ..通过执行以后的操作可以获得jpeg。

http://code.google.com/p/wkhtmltopdf/

http://code.google.com/p/wkhtmltopdf/

#5


0  

is it possible to convert html to image. However, first you must convert to PDF. see link

是否可以将html转换为图像。但是,首先您必须转换为PDF。看到链接