modperl_automatically_creating_and_caching_thumbnails

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



To: Rufus.Cable@icollector.com
From: Stas Bekman <stas@stason.org>
Subject: Re: RFC: Apache::Thumbnail - Generate image
thumbnails on the fly
Date: Fri, 8 Dec 2000 18:13:29 +0100 (CET)

On Fri, 8 Dec 2000 Rufus.Cable@icollector.com wrote:

> Hi -
> 
> Attached is an Apache module to automatically generate & cache thumbnail
> images. It uses Image::Magick (http://www.simplesystems.org/ImageMagick/)
> and File::Path (CPAN) and runs quickly enough to be useful on my
> Apache/mod_perl machine. It's my first proper handler, so let me know if
> there are any glaring ommissions or bugs! :)
> 
> Otherwise, if it's of use to anyone else, should I package this up and
> submit it to CPAN as v1.0? Comments welcome!

Check out these two first:

cpan> i /Apache::Album/
Module id = Apache::Album
    CPAN_USERID  WOODY (Jim Woodgate <woody@bga.com>)
    CPAN_VERSION 0.94
    CPAN_FILE    W/WO/WOODY/Apache-Album-0.94.tar.gz
    INST_FILE    (not installed)

cpan> i /Apache::Magick/
Module id = Apache::Magick
    DESCRIPTION  Image conversion on-the-fly
    CPAN_USERID  MPB (mod_perl book (Doug and Lincoln) <book@modperl.com>)
    CPAN_VERSION undef
    CPAN_FILE    Contact Author mod_perl book (Doug and
Lincoln) <book@modperl.com>
    DSLI_STATUS  bdpf (beta,developer,perl,functions)
    INST_FILE    (not installed)




===


To: modperl@apache.org
From: Rufus.Cable@icollector.com
Subject: RFC: Apache::Thumbnail - Generate image thumbnails
on the fly
Date: Fri, 8 Dec 2000 17:10:57 -0000 

------_=_NextPart_000_01C06139.D4D96120
Content-Type: text/plain;
	charset="iso-8859-1"

Hi -

Attached is an Apache module to automatically generate & cache thumbnail
images. It uses Image::Magick (http://www.simplesystems.org/ImageMagick/)
and File::Path (CPAN) and runs quickly enough to be useful on my
Apache/mod_perl machine. It's my first proper handler, so let me know if
there are any glaring ommissions or bugs! :)

Otherwise, if it's of use to anyone else, should I package this up and
submit it to CPAN as v1.0? Comments welcome!

Rufus.


------_=_NextPart_000_01C06139.D4D96120
Content-Type: application/octet-stream;
	name="Thumbnail.pm"
Content-Disposition: attachment;
	filename="Thumbnail.pm"

package Apache::Thumbnail;
use Apache::Constants qw(:common);
use Image::Magick ();
use File::Path ();

use strict;

use vars qw($VERSION);

$VERSION = '0.5';

sub handler {
	my $r = shift;

	my $root = $r->document_root();
	my $dest = $r->dir_config('Thumbnail_Directory') || '/thumbnails';
	my $geom = $r->dir_config('Thumbnail_Geometry') || '100x100';
	my $img = $r->filename().$r->path_info();
	$img =~ s/^$root\/[^\/]+//;

	# get thumbnail name
	my $thumb = $img;

	# final filenames
	my $srcFile = "$root$img";
	my $destFile = "$root$dest$thumb";

	# already cached?
	if (-f $destFile and -r _) {
		if (-C _ < -C $srcFile) {
			$r->internal_redirect("$dest$thumb");
			return OK;
		}
	}
	
	# is source image there?
	if (-s "$srcFile") {
		# yes, create thumbnail
		my $e;
		my $i = new Image::Magick;
		# read source
		$e = $i->Read("$srcFile");
		if ($e) {
			$r->log_error("Couldn't read '$srcFile': $e");
			return SERVER_ERROR;
		}
		# scale it
		$e = $i->Scale(geometry => $geom);
		if ($e) {
			$r->log_error("Couldn't scale '$srcFile': $e");
			return SERVER_ERROR;
		}
		# check directory tree is in place...
		my $path = $destFile;
		$path =~ s/\/[^\/]+$//o;
		if (!-d $path) {
			File::Path::mkpath $path ||
				($r->log_error("Couldn't create directory '$path': $!") && return SERVER_ERROR);
		}
		# write thumbnail
		$e = $i->Write($destFile);
		if ($e) {
			$r->log_error("Couldn't write '$destFile': $e");
			return SERVER_ERROR;
		}
		# and send to browser
		$r->internal_redirect("$dest$thumb");
		return OK;
	}
	
	# couldn't find source image
	return NOT_FOUND;
}

1;

__END__

=head1 NAME

Apache::Thumbnail - Generate image thumbnails on the fly

=head1 SYNOPSIS

   # in httpd.conf or similar

   <Location /thumbs/>
      SetHandler perl-script
      PerlSetVar Thumbnail_Directory "/thumbnails"
      PerlSetVar Thumbnail_Geometry "100x100"
      PerlHandler Apache::Thumbnail
      order allow,deny
      allow from all
   </Location>

   <Location /smallpics/>
      SetHandler perl-script
      PerlSetVar Thumbnail_Directory "/smallpics/"
      PerlSetVar Thumbnail_Geometry "200x200"
      PerlHandler Apache::Thumbnail
      order allow,deny
      allow from all
   </Location>

=head1 DESCRIPTION

This module automatically generates thumbnail images on the fly, caching
them in the specified thumbnail directory in a tree structure matching
that of the source images.

To use, set up a C<<Location>> directive as above. For any image on the
web server, a corresponding thumbnail is available by prepending that
location to the path - eg.

 Image at /images/staff/john.jpg

 Thumbnail at /thumbs/images/staff/john.jpg

That's it! The first time a thumbnail is requested, it'll be generated
and cached; subsequent requests will return the cached image immediately.
If the source image is newer than the thumbnail, the thumbnail will
automatically be regenerated.

=head1 OPTIONS

=over 4

=item Thumbnail_Directory

The directory in which the finished thumbnails are cached, relative to the
webserver document root. If unspecified, the default is C</thumbnails>.

 PerlSetVar Thumbnail_Directory "/thumbnails"

=item Thumbnail_Geometry

The maximum size of the thumbnail images. The proportions always remain
unchanged - see C<Image::Magick::Scale()> for details. If unspecified,
the default is 100x100 pixels.

 PerlSetVar Thumbnail_Geometry "100x100"

=back 4

=head1 BUGS

Doesn't currently check if the source image is smaller than the thumbnail
size, so tiny images would look nasty scaled up. Could add this, but it'd
be slightly slower, and probably not all that useful?

Another other bugs or suggestions, let me know!

=head1 PREREQUISITES

=item *
mod_perl L<http://perl.apache.org/>

=item *
Image::Magick L<http://www.simplesystems.org/ImageMagick/>

=head1 AUTHOR

Rufus Cable ruf@rcable.co.uk

=cut

===

the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu