0

I'm looking for a way to auto generate images (.png, .jpg, or .gif) from the output of a PHP string.

Ideally I'm looking for something that takes a string of HTML + CSS, or a URL, takes the output of that page with all the HTML / CSS formatting, and creates an image which retains the formatting, and then outputs that image file.

If CSS formatting isn't supported, pure HTML formatting or whatever the next best option is, would be fine.

Any suggestions?

Ali
  • 261,656
  • 265
  • 575
  • 769
  • Well you have a great rep, but what have you tried yet? Can you host VMs? You could run e.g. Firefox in a VM and take a screenshot. – rekire Jul 11 '12 at 20:44
  • 2
    [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/). Best. Library. Ever. (Yes I know it says PDF, it has a component that converts to images as well :-D). It's webkit based as well (hence the wk) so you're not working with some dodgy half-assed attempt at a rendering engine. – DaveRandom Jul 11 '12 at 20:45
  • @rekire I googled it and found some tutorials, however they all require the formatting to be hard-coded in the image generation code, which is a dealbreaker – Ali Jul 11 '12 at 20:47
  • @DaveRandom How can it be used with PHP? It seems like a shell utility. – Ali Jul 11 '12 at 20:48
  • It is, and it is very annoying that you can't pipe the input to its STDIN I'll admit. But I usually just create a local temp file and `exec()` it and it works no problem. Plus it will take a full URL as an argument, so if the content you want to convert can be served by a web server, you can just point it straight at that. – DaveRandom Jul 11 '12 at 20:49
  • @DaveRandom How would you install it? Where does it need to be put for exec() to work – Ali Jul 11 '12 at 20:51
  • Where are you trying to run this? If Linux shared hosting, I suspect its probably a no-go. If it's a box over which you have control, all you need to do is grab the code, compile it to a binary and shove it wherever you like as long as PHP can see it to execute it. On Windows you can just grab the installer package with pre-compiled binaries and off you go. – DaveRandom Jul 11 '12 at 20:55
  • @DaveRandom Its linux shared hosting. Even if it wasn't, no ideas how to compile it. – Ali Jul 11 '12 at 20:56
  • @ClickUpvote Sorry about that, clicked the chat link when I didn't mean to. You could ask your hosting company if they will allow you to use it if so they will probably compile it for you. AFAIK it's just a simple configure / make / make install job, but I've not actually compiled it on Linux myself (will do it now just to make sure I know how). It's worth asking your host, but if they say no I can't recommend anything else to use because I've never found anything else up to the job. – DaveRandom Jul 11 '12 at 21:03
  • HTML to PDF is well documented: http://stackoverflow.com/questions/391005/convert-html-css-to-pdf-with-php - maybe you can look into these libraries and their capabilities. Maybe you can take a two way approach with any of these. – madflow Jul 11 '12 at 21:05

0 Answers0