© copyright 09.Jan.2009 by Paul Bradley filed under PHP
The pet friendly cottage site I run, k9directory, uses lot's of thumbnails which are basically screen shots of the official web sites for each of the properties listed, see an example. These screen shots where taken at the time the properties registered with directory, as you can imagine over time these screen shot thumbnails became outdated as the hotel owners changed their own web sites. I needed to devise an automatic solution to updating these thumbnails, so that a procedure could be run on a monthly basis with little interaction. This article explains how I do it.
To able to batch automate this process on a Windows machine, I needed to find a command line tool that could be called from a DOS batch file which would capture the web site image to JPEG file for a give url.
After trying out different solutions I settled on the open source project run by Björn Höhrmann called IECapt, which allows to you to capture the rendering of a web page to either a JPEG or PNG file.
The syntax for the IECapt command is quite simple, you pass in the url you want to capture in the example below url2.org, and specify the output file to be created, e.g. url2.jpg. Other options include a min-width setting which specifies the minimal width of the resulting image file; and a delay option measured in seconds, which tells the program to wait 5 seconds before capturing the image. This allows any Javascript or Flash animations to be run on the target web site.
IECapt --url=http://url2.org --out=url2.jpg
--min-width=1024 --delay=5 --silent
n.b. the command should be entered on one line, split here for presentation.
Below is some sample PHP code I use to create the batchIECapt.bat file; it selects all the records from the main property database exporting the database id and the URL of the hotels web site. It loops through all the records in the table and outputs one line to the batch file for each of the records in the database, using the database id as the JPEG name. Once this has been run, I have the DOS batch file batchIECapt.bat which contains several hundred web sites to process. I simply set it running over night. In the morning I have a folder full of screenshots waiting to be thumbnailed.
<?php
error_reporting(E_ALL);
include 'mysql.php';
$_output_folder = 'H:/iecapt/';
$db = new dbmysql();
$sql = 'SELECT id, url FROM property';
$fh = fopen($_output_folder . 'batchIECapt.bat', 'w');
fwrite($fh, "@echo off" . "\n");
fwrite($fh, "del *.jpg" . "\n");
$db->prepare($sql);
$results = array();
$results = $db->execute(TRUE);
foreach ($results as $dbrow) {
fwrite($fh, 'IECapt --url=http://' . $dbrow[ 'url '] .
' --out=' . $dbrow['id'] .
'.jpg --min-width=1024 --delay=5 --silent' . "
");
}
fclose($fh);
$db->disconnect();
?>
The last step in the process is to automatically create a scaled down thumbnail of the original images created by IECapt. The original images where captured to a folder called H:/iecapt, and the PHP script below goes through each of the images in that folder and uses the PHP GD functions to create a thumbnail with the same name in a new folder called H:/iecapt-resize/200x/.
This sample PHP script is intended to be run from the command line, and if you want to use it simply change the $input_folder and $output_folder options to match your requirements. The $new_image_width setting is the width all the newly created thumbnails will have, the height will be kept to an aspect ratio which reduces blurring. You can reduce the $image_quality setting to something like 75% if you want to reduce the size of the thumbnail images; however this will lower the overall quality of the thumbnail and may cause excessive blurring.
<?php
error_reporting(E_ALL | E_STRICT);
$input_folder = 'H:/iecapt/';
$output_folder = 'H:/iecapt-resize/200x/';
$new_image_width= 200;
$image_quality = 100;
if(is_dir($input_folder) == TRUE) {
$files = array();
$files = scandir($input_folder);
foreach($files as $file) {
$fileInfo = explode('.', $file);
$fileSuffix = array_pop($fileInfo);
if($fileSuffix == 'jpg') {
echo 'Working on : ', $file, "\n";
$src_img = imagecreatefromjpeg(
$input_folder . $file);
$old_x = imageSX($src_img);
$old_y = imageSY($src_img);
$ratio = $new_image_width / $old_x;
$new_w = abs($old_y * $ratio);
$gd_img = imagecreatetruecolor(
$new_image_width, number_format($new_w));
imagecopyresampled($gd_img, $src_img, 0, 0, 0, 0,
$new_image_width, number_format($new_w),
$old_x, $old_y);
imagejpeg($gd_img, $output_folder .
$file, $image_quality);
imagedestroy($gd_img);
}
}
} else {
echo $input_folder, ' is not a directory.';
exit;
}
?>
About the Author
Paul Bradley is a VB.NET software developer living and working in Cumbria. He provides PHP & MySQL bespoke development services via his software development company, Carlisle Software Limited.
He has over 20 years programming experience.