Hey There, you have 12 shots left today! Your ip address (38.107.179.216) is being recorded.

Taking screenshots of websites with PHP (Website Thumbnails)

This little app is created using the Open Source program wkhtmltoimage. You could run this on your own headless linux server (X-Server not required!) for free, and generate as many website thumbnails as you wish. wkhtmltoimage uses webkit to render the HTML, and does a pretty damn good job of it too. This is one of the few solutions available that doesn't require Windows, which is why we use it here at Catch. I've tested it on RHEL 5/CentOS without any problems.

How

Step 1: Install wkhtmltoimage

First you will need to compile/install wkhtmltoimage on your server. We communicate directly with this binary using PHP's shell_exec() function. In our case, we used the precompiled binary (wkhtmltoimage-0.11.0_rc1 i386) located at http://code.google.com/p/wkhtmltopdf/downloads/list. You may run into some dependency issues when first running the binary. Just install them and you should be good to go.

# cd /tmp
# wget http://wkhtmltopdf.googlecode.com/files/wkhtmltoimage-0.11.0_rc1-static-i386.tar.bz2
# tar xvjf ./wkhtmltoimage-0.11.0_rc1-static-i386.tar.bz2
# chown apache:apache ./wkhtmltoimage-i386
# chmod +x ./wkhtmltoimage-i386
# mv ./wkhtmltoimage-i386 ./usr/bin/wkhtmltoimage-i386
# cd ~

wkhtmltoimage-i386 -V should now output version information.

Step 2: Integrate wkhtmltoimage with your web application

This part is fairly straightforward. I'm leaving out all the steps to set permissions etc, but of course PHP will need permission to communicate with wkhtmltoimage-i386 and wkhtmltoimage-i386 should have permission to output the image to your selected folder. This is merely a proof of concept.

    // The URL to get your HTML
    $url = "http://www.google.com/";
    
    // Name of your output image
    $name = "example.jpg";

    // Command to execute
    $command = "/usr/bin/wkhtmltoimage-i386 --load-error-handling ignore";

    // Directory for the image to be saved
    $image_dir = "/var/www/images/";
    
    // Putting together the command for `shell_exec()`
    $ex = "$command $url" . $image_dir . $name;

    // Generate the image
    // NOTE: Don't forget to `escapeshellarg()` any user input!
    $output = shell_exec($ex);


Check out wkhtmltoimage's Google Code wiki for details on PHP integration. They even include a complete script that is ready to go, assuming you have installed the binaries already. The class provided is pretty easy to use, although I had some dependency issues when trying to get wkhtmltoimage to actually run (Thank you Google).

Enter the URL to take a screenshot of:

Notice: Large/slow websites can take a few seconds to load.