Generating Website Thumbnails in PHP

In recent web browser developement, one of the new features is the generation of site thumbnail images displayed in a gallery. In Opera, it has a feature called Speed Dial Thumbnail, in Google Chrome, it has a page that displays thumbnail for Most Visited, Recent Bookmarks and Recently closed tabs, while in Internet Explorer 7, they call this feature as Quick Tabs. This promotes easier recall for users rather than remembering the exact URL address.

The following codes below can replicate the same thumbnail generation feature in PHP using only Internet Explorer browser. This is helpful if you wish to advertise your sponsors' sites or display other webpages that you want to recommend.

//*** PHP 5 with GD is required for this to work and IE client browser

function thumb_image($img_file,$url)
{
//** initiate a browser session for screen capture
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Fullscreen = true;
$browser->Navigate($url);

while ($browser->Busy) {
com_message_pump(4000);
}
sleep(1);

$im = imagegrabscreen(); //** works only with IE, captures the whole screen
$browser->Quit();
imagepng($im, "tmp_".$img_file); //** store workfile

$percent = 0.20; //** factor of thumbnail reduction = 20%
list($width, $height) = getimagesize("tmp_".$img_file);
$new_width = floor($width * $percent);
$new_height = floor($height * $percent);

//// Resample$im2 = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($im2, $im, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

imagepng($im2, $img_file); //** save url image into specified file_name
imagedestroy($im); imagedestroy($im2);
}

//*** SAMPLE USAGE
thumb_image("facebook.png","http://www.facebook.com"); /
/** call the function
print ""; echo "

";
//** use captured image like any regular picture
?>

No comments: