3

Is there a php script that allow to convert a html code to a png image. The problem with GD library is that only convert text to image( and not html code )

8
  • Could you explain more? html is text. Commented Aug 10, 2016 at 13:40
  • 1
    @BeetleJuice I believe that he wants to take HTML, render it, and then save the rendered HTML as a png image. Commented Aug 10, 2016 at 13:41
  • Did you check stackoverflow.com/questions/2651173/… ? Commented Aug 10, 2016 at 13:43
  • 1
    I think you might be better converting to PDF using something like wkhtmltopdf.org then you may have a better chance of converting PDF to an image. Commented Aug 10, 2016 at 13:43
  • 2
    @BeetleJuice A better example is '<img src='mylittlepony.gif'>'. In text form, you see letters. In rendered form, you see a picture. I doubt he wants to see the text. He wants to see the image. Basically, he wants to automate a screen shot of a web page. He is likely making a web page thumbnail program. Commented Aug 10, 2016 at 13:46

1 Answer 1

8

There is no pure PHP solution to this.

Basically you've got two options:

  1. Client-side rendering
    Use something like html2canvas to render your image using javascript in the browser.
  2. Server-side rendering
    Use a library like wkhtmltopdf which can be invoked by PHP to generate the image.
    Another server-side solution would be using Phantom.js which can also run javascript before capturing the screen.
Sign up to request clarification or add additional context in comments.

2 Comments

Client side rendering via html2canvas may face CORS issues which may prevent you from capturing images.
There's a new solution available: developers.google.com/web/updates/2017/04/… Will update my answer soon.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.