3 Comments

In order for this to work you will need wkhtmltopdf and the imagemagick tool convert.

On ubuntu you will just do:

sudo aptitude install imagemagick wkhtmltopdf
 
<?php
 
// save this snippet as url_to_png.php
// usage: php url_to_png.php http://example.com
if (!isset($argv[1])){
    die("specify site: e.g. http://example.com\n");
}
 
$md5 = md5($argv[1]);
$command = "wkhtmltopdf $argv[1] $md5.pdf";
exec($command, $output, $ret);
if ($ret) {
    echo "error fetching screen dump\n";
    die;
}
 
$command = "convert $md5.pdf -append $md5.png";
exec($command, $output, $ret);
if ($ret){
    echo "Error converting\n";
    die;
}
 
echo "Conversion compleated: $argv[1] converted to $md5.png\n"; 
 

This page has been viewed 9229 times. First hit: 26-Mar-2013 00:31:53
Share a link to this entry via email, Google+, Twitter, Facebook.

Comments

#

8

Really fantastic; thanks!

I changed the convert line to the following to improve the quality, since the defaults of the script resulted in a shrunk down PNG compared to how it's really displayed.

$command = "convert -density 110 -depth 8 -quality 85 -trim $argv[1].pdf -append $argv[1].png";

Taking density to about 125 is probably closer to real size, but I let it scale down just a little. I also activated -trim to automatically crop the image (otherwise you end up with a ton of whitespace on some sites).

Great stuff! Robbie

Reply ➘

#

1

or you use the wkhtmltoimage tool, which will work the same way as wkhtmltopdf

Reply ➘

#

-1

Where is the file saved?

Reply ➘

#

1

If all is OK, should be in the same path where the script is.