Thursday, May 31, 2012

Website screenshots using PHP

Is there any way of taking a screenshot of a website in PHP, then saving it to a file?

Source: Tips4all


  1. Sure you can, but you'll need to render the page with something.
    If you really want to only use php, i suggest you HTMLTOPS, wich render the page and output it in a ps file (ghostscript), then, convert it in a .jpg, .png, .pdf.. can be little slower with complex pages (and dont support all the CSS).

    Else, you can use wkhtmltopdf to output a html page in pdf, jpg, whaterver..
    Accept CSS2.0, use the webkit (safari's wrapper) to render the page.. so should be fine.
    You have to install it on your server, as well..

    UPDATE Now, with new HTML5 and JS feature, is also possible to render the page into a canvas object using Javascript. Here a nice library to do that: Html2Canvas and here an implementatio by the same author to get a feedback like G+.
    Once you have rendered the dom into the canvas, you can then send to the server via ajax and save it as a jpg.

    EDIT: You can use the imagemagick tool for transforming pdf to png. My version of wkhtmltopdf does not support images. E.g. convert html.pdf -append html.png.

    EDIT: This small shell script gives a simple / but working usage example on linux with php5-cli and the tools mentioned above.

    EDIT: i noticed now that the wkhtmltopdf team is working on another project: wkhtmltoimage, that gives you the jpg directly

  2. Since PHP 5.2.2 it is possible, to capture a website with PHP solely!

    imagegrabscreen — Captures the whole screen

    $img = imagegrabscreen();
    imagepng($img, 'screenshot.png');

    imagegrabwindow - Grabs a window or its client area using a windows handle (HWND property in COM instance)

    $Browser = new COM('InternetExplorer.Application');
    $Browserhandle = $Browser->HWND;
    $Browser->Visible = true;
    $Browser->Fullscreen = true;


    $img = imagegrabwindow($Browserhandle, 0);
    imagepng($img, 'screenshot.png');

    Edit: Note, these functions are available on Windows systems ONLY!

  3. This ought to be good for you:

    Make sure you download the wkhtmltoimage distribution!

  4. It's in Python, but going over the documentation and code you can see exactly how this is done. If you can run python, then it's a ready-made solution for you:

    Note that everything can run on one machine for one platform, or one machine with virtual machines running the other platforms.

    Free, open source, scroll to bottom of page for links to documentation, source code, and other information.

  5. Yes. You will need some things tho:

    See khtmld(aemon) on *nx.
    See Url2Jpg for Windows but since it is dotNet app you should also chek Url2Bmp

    Both are console tools that u can utilise from your web app to get the screenshot.

    There are also web services that offer it. Check this out for example.


    This link is useful to.

  6. A website has to be rendered. PHP does not have website (HTML, CSS, JavaScript, etc) rendering capabilities. So no.

  7. Not directly. Software such as Selenium have features like this and can be controlled by PHP but have other dependencys (such as running their java-based server on the computer with the browser you want to screenshot)

  8. I've been digging for something that can handle this and I came across this simple PHP class that's absolutely amazing!

  9. I'm on Windows so I was able to use the imagegrabwindow function after reading the tip on here from stephan. I added in cropping (to get rid of the Browser header, scroll bars, etc.) and resizing to get a final image. Here's my code. Hope that helps someone.

  10. Shrink the web has always been what I've used. I think they have a nice API.

  11. Try this:

  12. webkit2html works on Mac OS X and Linux, is quite simple to install and to use. See this tutorial.

    For Windows, you can go with CutyCapt, which has similar functionality.

  13. I modified webshot, it can be run from PHP with exec

  14. I used bluga. The api allows you to take 100 snapshots a month without paying, but sometimes it uses more than 1 credit for a single page. I just finished upgrading a drupal module, Bluga WebThumbs to drupal 7 which allows you to print a thumbnail in a template or input filter.

    The main advantage to using this api is that it allows you to specify browser dimensions in case you use adaptive css, so I am using it to get renderings for the mobile and tablet layout as well as the regular one.

    There are api clients for the following languages:

    .Net C#,
    and Bash (the shell script looks like it requires perl)

  15. Yes it is. If you only need image of URL try this

    <img src=''>

    Pass the url as argument and you'll get the image for more details check this link

  16. You can use



  17. Try GrabzIt they have a great API and thier service is very fast. I use it on one of my personal websites.

    Also because they use a callback when the screenshot is finished you don't have to continously poll them like you do for thumbilizer.

    They have simple instructions on their website, to request a screenshot be taken:


    //Create the GrabzItClient class
    //Replace "APPLICATION KEY", "APPLICATION SECRET" with the values from your account!
    $grabzIt = new GrabzItClient("APPLICATION KEY", "APPLICATION SECRET");
    //Take the picture the method will return the unique identifier assigned to this task
    $id = $grabzIt->TakePicture("", "");

    Then to save that screenshot create a PHP page to accept the screenshot result:


    $message = $_GET["message"];
    $customId = $_GET["customid"];
    $id = $_GET["id"];
    $filename = $_GET["filename"];

    $grabzIt = new GrabzItClient("APPLICATION KEY", "APPLICATION SECRET");
    $result = $grabzIt->GetPicture($id);

    if (!$result)

    //Ensure that the application has the correct rights for this directory.
    file_put_contents("images" . DIRECTORY_SEPARATOR . $filename, $result);

  18. Guys, just use script bellow

    echo '<script language="javascript" src="'.urlencode($url).'"></script>';