Convert HTML to Image in PHP without shell

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.

Leave a Comment