Take screenshot of div using PHP and GD

Is it possible to take a screenshot of just a DIV, using GD and PHP? I am trying to automate a “preview” of content and want to screenshot a particular DIV and save it as a preview instead of loading the content (because I need thumbnails, etc.)

Is it possible? Anyone seen it done before?

I know something like that is possible, because websites like BrowserCam do screenshots and show thumbnails.

No it is no possible (not easy anyways), you need to query the graphics system provided by the OS which is an image of the current screen that was produce by the graphics card, etc etc. The crop out the secintion with the rendered div, like asking a blind man to do a heart transplant.

But the simple answer is, no there is no means to get the display of a div with PHP and GD.

You want to build something like Snap or BrowserCam does ?

If this is what you mean, I think this should have something to do with things other than PHP and GD, since how a div looks completely depends on the browser. This can be about screen capturing. So there must be a real machine running a browser and a screen capturing software I think.

I also wondered how this is done, and sent a message to Snap a few secs ago :slight_smile:


It can be possible, but it can require a complex interaction between the browser and the capturing software. Such as browser sending the rendered coordinates of the div etc.

Thats why in parenthesis i put “not possible (not easy anyways)”.

I would create this in something other then PHP something that actually has the means to interact with the OS seamlessly.

PHP is just not designed to do this.

You need a HTML rendering engine. Theoretically, you should be able to script the mozilla rendering engine to do something like that, but it’ll take some serious hacking to get running. On windows, you can also script the IE browser directly, which could probably do something similar.
Another option would be to script the OS itself and take a screenshot. You could probably use something like WINE or Wmware for that. Again, all theoretically speaking.


You are right. When I say “it should be possible”, I mean methods other than PHP, as you say :slight_smile:

And a note. PHP can be used to build desktop applications (GTK etc). But whether this is the best way of doing things or not can be questionable.

There have been a few questions about this on the ImageMagick forum ( search results for screenshot = http://redux.imagemagick.org/discourse-server/search.php?keywords=screenshot&terms=all&author=&fid[]=1&sc=1&sf=all&sk=t&sd=d&sr=posts&st=0&ch=200&sid=5e65e678773053f70a2bf2e64b6085bf&t=0&submit=Search ) the best answer is probably this one http://redux.imagemagick.org/discourse-server/viewtopic.php?f=1&t=7104&hilit=screenshot
I have looked into html2ps which is a Perl script but there is a php version but I could not get it to work.

There are a couple of open source solutions for whole pages, but they have some real limitations. One of them uses part of KDE to get a screen buffer image from Mozilla. The problem is that there is no indication from the browser, when the page is finished loading the page, the snap is done blind with a delay in the script to hopefully let the browser load the page in time.

Anything that doesn’t use a browser engine would have questionable layout quality.

Until somebody embeds the gekko engine into some sort of PHP extension and provides display buffer control or some sort of printer driver for this purpose, you will need something custom.

Thanks for the advice… seems it’s not really possible (we’re on a 'nix server with no X) so we’re looking at different route.

For what it’s worth – we weren’t going to create any kind of service, we just wanted a thumbnail preview for content inside a CMS. Oh well…

How about making that div load in a popup that can be screen shot separately?

My only guess is that you would employ a windows based system, then use COM/Some other type of interaction to communicate with the system and various browsers. However, given the nature of having a browser doing that constantly, I question the performance sanity…