Screenhot einer Website über die Konsole machen
Aus HackerWiki
Contents |
Einrichtung und Benutzung des Webthumbnailers
Was wird benötigt?
Folgende Pakete müssen für einen reibungslosen Ablauf installiert sein:
- xvfb (der verwendete X-Server, in dem unser Browser startet)
- xfonts-100dpi, xfonts-75dpi, xfonts-base, xfonts-encodings, xfonts-scalable, xfonts-utils, ttf-baekmuk, ttf-bitstream-vera, ttf-dejavu, ttf-freefont (notwendige Schriftarten)
- firefox (wir nutzen Firefox als Browser)
- mplayer (für Multimedia-Inhalte auf einer Seite)
- imagemagick (convert)
- libjpeg-progs (cjpeg)
Zusätzlich muss der Firefox mit folgenden Plugins ausgestattet werden:
- Autohide - http://www.krickelkrackel.de/autohide/autohide_de.htm (sorgt für einen echten Fullscreen ohne störende Leisten)
- FlashPlugin - http://www.adobe.com/ (zur Anzeige von Flash-Inhalten einer Seite)
- Mplayer-Plugin - http://ftp.br.debian.org/debian/pool/main/m/mplayerplug-in/mozilla-mplayer_3.45-1_i386.deb
Firefox anpassen
Nachdem die Plugins installiert sind, müssen noch ein paar Modifikationen gemacht werden. Zuerstmal muss das Session-Restore deaktiviert werden. Dafür einen VNC-Server starten (oder einen lokalen XServer, wenn man physischen Zugriff auf die Maschine hat), Firefox aufrufen und im 'about:config' den Wert browser.sessionstore.resume_from_crash auf false setzen. Um die Scrollbar verschwinden zu lassen, wird im Default-Profil des FF die Datei chrome/userContent.css angelegt, die folgenden Inhalt erhält:
scrollbar { display: none !important; }
Das Skript
Folgendes Script ist dafür zuständig den Screenshot zu machen (es handelt sich dabei um ein etwas modifiziertes Webthumb-Skript):
#!/usr/bin/perl
my $xvfbCommand = "Xvfb :2 -screen 0 1024x1280x16"; # hier wird die initiale Groesse des Screenshots festgelegt
my $mozillaCommand = "firefox";
my $mozillaParam = "-fullscreen -width 1024 -height 768 --display=:2 --sync"; # firefox mag nicht im Hochkant-Format gestartet werden
my $mozillaWidth = "1024";
my $mozillaHeight = "768";
my $background = "black";
my $loadTime = 5;
my $httpOnly = 1;
my $files = $ENV{'HOME'} . "/.webthumb";
use POSIX;
if (int(@ARGV) != 1) {
die "Usage: webthumb URL\n" .
"Produces a ppm file on standard output, suitable for piping\n".
"to pnmscale, cjpeg, and so on.\n";
}
if (!(-e $files)) {
if (!mkdir($files)) {
die "Can't create $files to hold temporary files, home directory must be writable.\n";
}
}
&lock;
&restartIfNeeded("Xvfb", $xvfbCommand);
$ENV{'DISPLAY'} = ":2";
system("xsetroot -solid $background");
&restartIfNeeded("firefox", "$mozillaCommand $mozillaParam");
my $url = $ARGV[0];
if ($httpOnly) {
if ($url !~ /^http\:/) {
die "Only HTTP URLs are allowed";
}
}
my $good = 0;
for ($tries = 0; ($tries < 10); $tries++) {
my $result = system($mozillaCommand, '-remote', "openURL($url)");
if ($result == 0) {
$good = 1;
last;
}
sleep(1);
}
if (!$good) {
die "$mozillaCommand -remote didn't work after 10 tries.";
}
sleep($loadTime);
system("xwd -root -silent -display :2 | xwdtopnm");
&unlock;
sub restartIfNeeded
{
my ($name, $command) = @_;
my $xpid;
if (open(IN, "$files/$name")) {
$xpid = <IN>;
close(IN);
if (!(-e "/proc/$xpid")) {
$xpid = undef;
} else {
open(IN, "/proc/$xpid/cmdline");
my $cmdline;
read(IN, $cmdline, 1024);
close(IN);
if ($cmdline !~ /webthumb/) {
$xpid = undef;
unlink("$files/$name");
}
}
}
if (!defined($xpid)) {
my $pid = fork;
if ($pid == 0) {
POSIX::setsid() || die "Can't disassociate $name process";
system($command);
sleep(1);
unlink("$files/$name");
exit 0;
} else {
$xpid = $pid;
open(OUT, ">$files/$name");
print OUT $xpid;
close(OUT);
sleep(2);
if (!(-e "$files/$name")) {
die "Unable to start $command";
}
}
}
}
use Fcntl ':flock';
sub lock
{
open(LOCK, ">>$files/lock") || die "Can't create $files/lock";
flock(LOCK, LOCK_EX);
}
sub unlock
{
flock(LOCK, LOCK_UN);
close(LOCK);
}
my $xproc = `ps ax | grep vfb | awk '{print $1}'`;
print "XProc: $xproc";
system("kill -9 $xproc");
Ein Thumbnail erstellen
Wie zu erkennen ist, erzeugen wir zuerst eine PPM-Datei, die wir noch in ein JPEG-Bild umwandeln muessen, wofür wir den Output des Skripts einfach an cjpeg weiterreichen:
./webthumb http://bitmuncher.blog.de/ | cjpeg > thumb.jpg
Das entstehende Bild koennen wir nun mit 'convert' in die Grösse eines Thumbnails bringen:
convert -size 1024x1280 -resize 60x75 thumb.jpg thumb1.jpg
Ein Screenshot einer Seite sieht nach korrekter Einrichtung des Thumbnailers dann wie folgt aus:
Und nach dem Konvertieren bekommen wir ein kleines Bild wie dieses:

