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.BeetleJuice– BeetleJuice2016-08-10 13:40:01 +00:00Commented 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.kainaw– kainaw2016-08-10 13:41:33 +00:00Commented Aug 10, 2016 at 13:41
- Did you check stackoverflow.com/questions/2651173/… ?sdabet– sdabet2016-08-10 13:43:25 +00:00Commented Aug 10, 2016 at 13:43
- 1I 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.SheppardDigital– SheppardDigital2016-08-10 13:43:26 +00:00Commented 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.kainaw– kainaw2016-08-10 13:46:24 +00:00Commented Aug 10, 2016 at 13:46
| Show 3 more comments
1 Answer
There is no pure PHP solution to this.
Basically you've got two options:
- Client-side rendering
Use something like html2canvas to render your image using javascript in the browser. - 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.
2 Comments
dev7
Client side rendering via html2canvas may face CORS issues which may prevent you from capturing images.
Anticom
There's a new solution available: developers.google.com/web/updates/2017/04/… Will update my answer soon.