Forum Moderators: coopster
Quick search on Google for website thumbnail generator php [google.com] returns few interesting entries which could be helpful (ready solutions for Windows and some info on Linux solutions).
Usually php script just runs some hidden renderer (usually browser like IE, Mozilla, Safari) and uses website snapshot generated by renderer.
Maybe imagemagick can also be used as "renderer".
I also seen one php script which was able to work without third party programs (it was parsing html itself). It was sooo slow (took about a minute on my pc to generate one website thumb), and websites were not rendered good. So i would not suggest using this.
GD2 is close to as good as IM in quality, but IM is, in my experience, very fast and efficient... I don't usually like to post a ton of code, but here's a thumbnail generator that I wrote years ago using IM. I only have the vaguest memory of this, but I think this is all you need... then again something might be missing.
// The core function is makeThumbnail, which calls getImageInfo().
// set parameters for the local filesystem and for desired sizes and quality.
define ('IM_CONVERT', '/usr/local/ImageMagick/bin/convert'); // path to convert executable
define ('IM_IDENTIFY', '/usr/local/ImageMagick/bin/identify'); // system path to the identify executabledefine('IMAGE_QUALITY', 80); // jpeg qual level for image compression
define('MAX_IMAGE_WIDTH', 225);
define('MAX_IMAGE_HEIGHT', 300);
define('MAX_IMAGE_FILESIZE', 100); // size in KBfunction makeThumbnail($lg_image, $thumb_width, $thumb_height, $thumb_quality = IMAGE_QUALITY) {
// get our paths, filenames, etc set up
$lg_image_name = basename($lg_image);
$image_dir = dirname($lg_image);
$name_array = explode(".", $lg_image_name);
$extension = array_pop($name_array);if ($image_dir!= ".")
{
$thumbnail = $image_dir . "/";
}
else
{
$thumbnail = "";
}$thumbnail .= implode(".", $name_array) . "_tn." . $extension;
// now make the thumbs with ImageMagick
Exec(IM_CONVERT . ' -size "'.$thumb_width .'x' . $thumb_height . '"'
. ' -quality "' . $thumb_quality . '" '
. '"' . $lg_image . '"'
. ' -resize "' . $thumb_width . "x" . $thumb_height . '" +profile "*" '
. '"' . $thumbnail . '"');// check for success
if (!file_exists($thumbnail))
{
return false;
}// we make a last check for filesize and recurse if necessary.
// If too large, we knock down the jpeg quality by 10 and keep going until
// the file meets our max file size requirement$image_info = getImageInfo($thumbnail);
@unlink($lg_image); // we suppress errors b/c this won't work if IM still has control of the file;if (!empty($image_info) && $image_info['size'] > MAX_IMAGE_FILESIZE * 1024)
{
$more_info = GetImageSize($thumbnail);
// if it's a jpeg (2, 9, 10) we'll try to resize down again.
if ($more_info[2] == 2 ¦¦ $more_info[2] == 9 ¦¦ $more_info[2] == 10)
{
makeThumbnail($thumbnail, $thumb_width, $thumb_height, ($thumb_quality - 10));
}
else
{
// if it's not, we'll make it smaller
$thumb_width = ceil($thumb_width / 2);
$thumb_height = ceil($thumb_height / 2);
makeThumbnail($thumbnail, $thumb_width, $thumb_height, $thumb_quality);
}}
else
{
return $thumbnail;
}
}// We need to get information about pixel size and file size
// se we can verify that the file meets our requirementsfunction getImageInfo($image) {
// %w = width, %h = height, %b = size in bytes
$image_data_string = Exec(IM_IDENTIFY . " -format \"%w %h %b\" \"$image\"", $output, $return_code);
if (empty($output[0]))
{
return false;
}
$image_data_tmp = explode(" ", $output[0]);
$image_data = array('width' => $image_data_tmp[0],
'height' => $image_data_tmp[1],
'size' => (int) $image_data_tmp[2]);
return $image_data;
}// this is more or less a wrapper to simplify resizing an image using makeThumbnail()
function resizeImage($image)
{
$image_info = getImageInfo($image);
if (empty($image_info))
{
return false;
}if ( $image_info['width'] > MAX_IMAGE_WIDTH
¦¦ $image_info['height'] > MAX_IMAGE_HEIGHT
¦¦ $image_info['size'] > MAX_IMAGE_FILESIZE)
{
$thumbnail = makeThumbnail($image, MAX_IMAGE_WIDTH, MAX_IMAGE_HEIGHT, IMAGE_QUALITY);
}if (empty($thumbnail))
{
return $image;
}$name = str_replace('_tn', '', $thumbnail);
if(file_exists($thumbnail) &&!file_exists($name))
{
rename($thumbnail, $name);
return($name);
}
return $thumbnail;
}
The process they use is basically to startup mozilla in a virtual xwindows session, and screen grab the window id of the browser.
Combine this with the above and we have the generator.
To be honest though would prefer a cleaner solution :)