Forum Moderators: coopster

Message Too Old, No Replies

Looking for a PHP library to make websites thumbnails

         

guarriman

2:53 pm on Jul 5, 2007 (gmt 0)

10+ Year Member



Hi.

I'm looking for a PHP library (or a savvy tip) to get snapshots/thumbnails from a website, as shown in <snip>

Any suggestion? Thank you very much.

[edited by: eelixduppy at 5:03 pm (utc) on July 5, 2007]
[edit reason] removed url [/edit]

ChrisDean

3:23 pm on Jul 5, 2007 (gmt 0)

10+ Year Member



Have seen some implementations in Perl using imagemagick on Google code

And will probably soon write one myself cloning this functionality if possible.

If you want a copy of this code when I do it just ask, or if you want to collaborate on writing it let me know :)

Chris Dean

joelgreen

4:21 pm on Jul 5, 2007 (gmt 0)

10+ Year Member



Could not find solution you mentioned on Google Code (perl + imagemagick).

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.

ergophobe

11:41 pm on Jul 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just call imagemagick convert binary from PHP (i.e. using exec() or something).

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 executable

define('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 KB

function 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 requirements

function 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;
}


ChrisDean

9:03 am on Jul 6, 2007 (gmt 0)

10+ Year Member



There's details here of how to use a shell script plus mozilla, imagemagick and vnc to take a screenshot of a webpage

[studio.imagemagick.org...]

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 :)

ergophobe

3:13 pm on Jul 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry, I didn't read carefully enough - I didn't realize that you were looking to grab screenshots of websites.

In that case, you would probably want to do some sort of 100% shell script. There's no real reason to involve PHP and Apache, is there?

ChrisDean

8:55 pm on Jul 9, 2007 (gmt 0)

10+ Year Member



I agree that you could do the lot with a shell script but should you ever want to to add some web based functionality having php tie ins from the start may be benificial to some people