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

18 comments:

  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

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


    imagegrabscreen — Captures the whole screen


    <?php
    $img = imagegrabscreen();
    imagepng($img, 'screenshot.png');
    ?>



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


    <?php
    $Browser = new COM('InternetExplorer.Application');
    $Browserhandle = $Browser->HWND;
    $Browser->Visible = true;
    $Browser->Fullscreen = true;
    $Browser->Navigate('http://www.stackoverflow.com');

    while($Browser->Busy){
    com_message_pump(4000);
    }

    $img = imagegrabwindow($Browserhandle, 0);
    $Browser->Quit();
    imagepng($img, 'screenshot.png');
    ?>


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

    ReplyDelete
  3. This ought to be good for you:

    http://code.google.com/p/wkhtmltopdf/

    Make sure you download the wkhtmltoimage distribution!

    ReplyDelete
  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:

    http://browsershots.org/

    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.

    ReplyDelete
  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.

    Edit:

    This link is useful to.

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

    ReplyDelete
  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)

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

    https://github.com/mptre/thumbalizr

    ReplyDelete
  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.

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

    ReplyDelete
  11. Try this:
    http://www.websnapr.com/

    ReplyDelete
  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.

    ReplyDelete
  13. I modified webshot, it can be run from PHP with exec https://github.com/umpirsky/webshot

    ReplyDelete
  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:

    PHP,
    Python,
    Ruby,
    Java,
    .Net C#,
    Perl
    and Bash (the shell script looks like it requires perl)

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

    <img src='http://zenithwebtechnologies.com.au/thumbnail.php?url=www.subway.com.au'>


    Pass the url as argument and you'll get the image for more details check this link http://zenithwebtechnologies.com.au/auto-thumbnail-generation-from-url.html

    ReplyDelete
  16. You can use wimg.ca

    example: http://wimg.ca/http://stackoverflow.com/questions/757675/website-screenshots-using-php

    :D

    ReplyDelete
  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:

    include("GrabzItClient.class.php");

    //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("http://www.google.com", "http://www.example.com/GrabzItHandler.php");


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

    include("GrabzItClient.class.php");

    $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)
    {
    return;
    }

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

    ReplyDelete
  18. Guys, just use script bellow

    <?php
    echo '<script language="javascript" src="http://www.bildesel.de/thumb.php?url='.urlencode($url).'"></script>';
    ?>

    ReplyDelete