6

How can you render and save a web page as an image in PHP, probably with a width of 600px. How can I render a page in PHP without using a browser? How can I save it with a given resolution and image format (jpeg)? The functionality is similar to Google Preview, except it will not be displayed in a rollover.

Similar to this question, which is answered in C#. How to save a web page as image

Thanks!

Community
  • 1
  • 1
B Seven
  • 44,484
  • 66
  • 240
  • 385
  • possible duplicate of [How to save webpage as a image file using PHP?](http://stackoverflow.com/questions/3175392/how-to-save-webpage-as-a-image-file-using-php) – Marc B May 03 '11 at 17:21
  • Cool. Thanks for all the great resources! – B Seven May 03 '11 at 17:31

2 Answers2

7

You should get wkhtmltoimage, which is very easy to utilize from within PHP:

exec("../wkhtmltoimage --crop-w 600 http://example.com/ output.png");

There are other options, and instead of --crop-w or --width 600 it might be better to downscale it using GD or imagemagick afterwards.

mario
  • 144,265
  • 20
  • 237
  • 291
  • Thanks. Looks like it only converts to PDF and PS. I guess I could use another library to convert to jpg... – B Seven May 03 '11 at 17:35
  • No there are two versions of it. The one you found is the `wkhtmltopdf` version. But there is also the `..toimage` binary, which is what you want. (Also simpler to use.) – mario May 03 '11 at 17:36
  • I found http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltoimage-0.10.0_beta2-static-amd64.tar.bz2&can=4&q= for wkhtltoimage, but it says version 0.10 and depricated. There is no documentation... – B Seven May 03 '11 at 17:52
  • That's the version I have installed `wkhtmltoimage 0.10.0 rc2`. The releases get stale fast, I assume that's why it was labeled deprecated. The documentation is indeed thin: http://code.google.com/p/wkhtmltopdf/wiki/IntegrationWithPhp – mario May 03 '11 at 17:55
  • Thanks. After doing extensive research, it looks like this is the only relatively simple solutions for unix. Mac and Windows have other solutions that seem easier(!). Also, if that doesn't work I can use wkhtmltopdf and then convert to jpeg using ImageMagick. It's not very efficient but it's free and workable. I wonder if it will look OK... – B Seven May 03 '11 at 18:15
  • Always gives me pretty sharp screenshots (without scaling that is). Btw, you can just use `output.jpeg` to get a JPEG image. And there is also a `.exe` version for Windows (which happens to be a bit speedier under Linux/`wine` for whatever reasons, haha). – mario May 03 '11 at 18:17
  • It worked! Super easy to use. I just uploaded the file and changed the permissions. I spent hours looking for solutions, and this was the only workable one I found for unix. Thank you! – B Seven May 05 '11 at 00:48
  • Yes, I also tried some of the awfully complex workarounds (Xfvb&firefox DISPLAY=:2), but ended up settling on this. It's quite standards compliant and gives as excellent rendering results as Chrome. The only downside is that it's not speedy. (But neither are the cumbersome solutions). -- Anyway, good to hear! – mario May 05 '11 at 00:51
2

You can't do this in pure PHP, you'll always need an external renderer to get a good result.

Your best bet would be to use an external service such as the browsershots.org API, this way you won't produce extra load on your server.

If you have resources to burn, you could use another method (still running on your server, just not PHP) called wkhtmltoimage as in mario's answer. Just remember that this wouldn't be recommended (infact, probably not even possible) on shared hosting.

Community
  • 1
  • 1
Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
  • That seems a nice API indeed. I'll check that out too (currently using a more flaky service in one project..) – mario May 03 '11 at 17:33