7

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!!

user1083320
  • 1,836
  • 8
  • 22
  • 29

5 Answers5

14

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.

Spudley
  • 166,037
  • 39
  • 233
  • 307
3

This is not possible in pure 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.

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).

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Alright, I am not fixed on using PHP. The page will display the content as HTML. I just want to give that option to the user, that if they wanted to download the page (this HTML is a calendar), they can then click a button, and the page is saved as image, and the user can then save it. – user1083320 Oct 23 '12 at 21:40
  • @user check out the links in [How to save webpage as a image file using PHP?](http://stackoverflow.com/q/3175392) – Pekka Oct 23 '12 at 21:42
  • @user1083320: Why does the image need to look like the HTML page? – Eric Oct 23 '12 at 21:45
  • How crucial is it that it's an image? You could make a decent print.css stylesheet and encourage users to print to PDF. – Rikki May 20 '15 at 13:00
1

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

Roger Gajraj
  • 523
  • 4
  • 12
0

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

Community
  • 1
  • 1
urzeit
  • 2,863
  • 20
  • 36
0

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

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

saad arshad
  • 259
  • 1
  • 10