How to Take a Screenshot of a Website with PHP and Gd

How can I take a screenshot of a page with GD?

Well you can use a command line tool to take a screenshot of a webpage. You can run the tool within Php using shell_exec function. For example the wkhtmltopdf command line tool allows converting from html to pdf and other formats.

If the web page contains complex JavaScript or Ajax, then you can use a headless browser to take the screenshot. For example see: Chrome Headless. Also see the Html2Canvas script. It allows taking screenshots using JavaScript.

Also see this question: Website screenshots

How can I generate a screenshot of a webpage using a server-side script?

You can probably write something similar to webkit2png, unless your server already runs Mac OS X.

UPDATE: I just saw the link to its Linux equivalent: khtml2png

See also:

  • Create screenshots of a web page using Python and QtWebKit
  • Taking automated webpage screenshots with embedded Mozilla

Create an image from an url using PHP GD

Pretty sure GD can't handle this by itself. You'd need an HTML renderer too.

Is it possible to take a screenshot of the rendered DOM on the server side?

You can use WKHTMLTOPDF. It also has the functionality to save images called WKHTMLTOIMAGE (I think that's a different distribution, but you can download it from the same link). You can run it with exec() from PHP as a system call. Using a virtual buffer like xvfb you can run the WKHTMLTOIMAGE headless on the server. It is WebKit based so the results produced are really good...

You can scale the resulting image with standard PHP functions like described for example in these posts:

  • dynamically scale images in php jpg/png/gif
  • Resize image using PHP if it's too small

You might want to check my answer to this question HTML2PDF in PHP - convert utilities & scripts - examples & demos, maybe it helps you too.



Related Topics



Leave a reply



Submit