Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a thumbnail of a website?

Tags:

webkit

gecko

Is would guess that there is already a webkit/gecko based command line app (maybe even working as a server speed up to rending of multiple pages) which is already doing this?

like image 906
Lothar Avatar asked Aug 24 '09 07:08

Lothar


1 Answers

Here's a sample Qt4 command line app which creates a screengrab of an entire web page - easy to adapt for thumbnail generation....

#include <QtGui/QApplication>
#include <QtCore/QCoreApplication>
#include <QtGui>
#include <QtWebKit>
#include <QWebPage>
#include <QTextStream>
#include <QSize>

QWebView *view;
QString outfile;

void QWebView::loadFinished(bool ok)
{
        QTextStream out(stdout);
        if (!ok) {
                out << "Page loading failed\n";
                return;
        }
        view->page()->setViewportSize(view->page()->currentFrame()->contentsSize());
        QImage *img = new QImage(view->page()->viewportSize(), QImage::Format_ARGB32);
        QPainter *paint = new QPainter(img);
        view->page()->currentFrame()->render(paint);
        paint->end();
        if(!img->save(outfile, "png"))
                out << "Save failure\n";
        QApplication::quit();
        return;
}

int main(int argc, char *argv[])
{
        QTextStream out(stdout);
        if(argc < 3) {
                out << "USAGE: " << argv[0] << " <url> <outfile>\n";
                return -1;
        }
        outfile = argv[2];
        QApplication app(argc, argv);
        view = new QWebView();
        view->load(QUrl(argv[1]));

        return app.exec();
}

You can run this on a server using xvfb too. See this blog post for original and a link to a python alternative.

like image 177
Paul Dixon Avatar answered Oct 01 '22 23:10

Paul Dixon