How to Create Website Thumbnails with PHP and Firefox
Page last updated on 2011 / 04 / 09Being able to create thumbnail images of websites can be particularly useful as a website owner, visitors, and generally aesthetically pleasing to appear on a page.
There are a number of ways you can create thumbnails, some better than others, while some lack fairly essential features like the ability to render flash before the thumbnail image is generated. Without that particular feature, flash website thumbnails appear like blank pages.
Here are two options that are available to you and can be adjusted accordingly, one exclusively for linux users:
PHP / Firefox
If you are a firefox user, I recommend getting the Pearl Crescent Page Saver plugin for Firefox. A free and paid version is available, with the latter offering slightly more features should you have the requirement of them.
The basic requirements of the script are:
- PHP: Or any other scripting language that can iterate through the list of URLs you would like to make thumbnails of
- Firefox: The browser that is used to render webpages you want to make thumbnails of
- Page Saver Plugin: The plugin that interacts with Firefox to generate a thumbnail
- ImageMagick: Not essential, but is very handy for post-processing of images, i.e. resizing.
- Access to the command line
<?php $sites = array('http://www.innvo.com/','http://www.yahoo.com/','http://www.google.co.uk/'); foreach($sites as $key => $site) { $file = './'.$key.'.jpg'; $string = 'firefox -saveoptions visible -captureflash -savedelay 1500 -width 1024 -height 768 -saveimage "'.$site.'" -saveas '.$file; `$string`; $string = 'convert '.$file.' -resize 300x180 _'.$key.'.jpg'; `$string`; } ?>
If you do not have Imagemagick installed, you will want to remove the last 2 lines of code as it involves resizing the image.
Note that you will want to close all your browser windows while testing out this script.
Bash / Konqueror
I am using Ubuntu, your flavour of Linux may require different commands. The following packages/software are required for the bash script to run correctly:
- apt-get install xvfb
- apt-get install netpbm
- apt-get install x11-apps
- apt-get install konqueror
- apt-get install fluxbox
- apt-get install imagemagick
Save the following as thumbnails.sh
#!/bin/bash pkill -x "(Xvfb)" & sleep 1; pkill -x "(konqueror)" & sleep 1; pkill -x "(fluxbox)" & sleep 1; Xvfb :10 -auth /etc/X11.hosts -once -screen 0 1024x768x24 & sleep 3; export DISPLAY=:10; fluxbox & sleep 0; konqueror --geometry 1024x740 & sleep 2; for i in `cat -T $1`; do URL=${i%^I*} URLID=${i#*^I} qdbus `qdbus | grep -m1 konqueror` /konqueror/MainWindow_1 org.kde.Konqueror.MainWindow.openUrl $URL false sleep 10; import -display :10 -w root $2$URLID.jpg; convert $2$URLID.jpg -crop 1000x578+2+138 $2$URLID.jpg; convert $2$URLID.jpg -resize 128x73 $2\_$URLID.jpg; convert $2$URLID.jpg -resize 300x180 $2$URLID.jpg; done; pkill -x "(konqueror)" & sleep 0; pkill -x "(fluxbox)" & sleep 0; pkill -x "(Xvfb)" & sleep 0;
Save the following as thumbnails.txt and also create a directory for the thumbnails to reside in, for example /var/www/thumbs/
http://www.google.com/ 1
http://www.yahoo.com/ 2
http://www.innvo.com/ 3
The following command will then iterate through the list in thumbnails.txt
sh thumbnails.sh thumbnails.list /var/www/thumbs/
Some notes regarding the latter script and both methods in general
- I use PHP to generate the thumbnails.txt input files, the bash script iterates through each line and accesses the $URL and saves the thumbs with the filename $URLID in each line
- Xvfb initiates a mock display that konqueror uses to render web pages. You don't actually need to see the browser working through the list
- You may want to have a default page and load that up before calling each URL. This is so that a 'default thumb' can be used when a webpage is very slow to load.
- You have to shut the browser down before running these scripts, otherwise browser invocation will complain that the browser is already running. This is why I like the Konqueror bash script more, as I use Firefox to browse.
Tweet