#!/usr/bin/perl -w # Location of the ImageGallery.pm module # (if not in the current path) use lib '/path/to/modules'; use ImageGallery; my $webapp = ImageGallery->new ( # Location of templates. TMPL_PATH => '/path/to/templates/', PARAMS => { # Full path to images photos_dir => '/path/to/photos', # Thumbnail size (s, m or l) thumb_size => 'm', # Number of thumbnails per row thumbs_per_row => 4, script_name => $0 } ); $webapp->run(); #### <TMPL_VAR NAME="title">
?r=1&i=">?r=1&i=&" alt="" />  

##
## <TMPL_VAR NAME="title">
?r=1&i=&f" alt="" width="" height="" />
##
## package ImageGallery; use base 'CGI::Application'; use File::Basename; use GD; use strict; sub setup { my $self = shift; $self->run_modes ( '0' => 'show_gallery', '1' => 'show_image', 'AUTOLOAD' => 'show_gallery' ); $self->start_mode('0'); $self->mode_param('r'); } sub show_gallery { my $self = shift; my $row_limit = $self->param('thumbs_per_row'); my %ok_ext = map { $_ => 1 } qw(jpg gif png); my $image_dir = $self->param('photos_dir'); my @dir_row; while(my $dir = <$image_dir/*>) { next unless -d $dir; my $dir_row = {dir => substr($dir, length($image_dir) + 1)}; my ($i, $j) = (0, 0); while (my $full = <$dir/*>) { my ($file, $ext) = (fileparse($full,keys %ok_ext))[0,2]; next unless $ok_ext{$ext}; push @{$dir_row->{file_row}->[$j]->{images}}, { filename => substr($full, length($self->param('photos_dir'))), alt => $file }; $j++ unless ++$i % $row_limit; } for ($i = $i; $i % $row_limit; $i++) { push @{$dir_row->{file_row}->[$j]->{images}}, { filename => '', alt => '' }; } push @dir_row, $dir_row; } my $html = $self->load_tmpl('photos_index.tmpl', global_vars => 1); $html->param(script_name => $self->param('script_name')); $html->param(thumb_size => $self->param('thumb_size')); $html->param(title => 'My Photo Gallery'); $html->param(dir_row => \@dir_row); return $html->output; } sub show_image { my $self = shift; my $q = $self->query(); my $image = GD::Image->new($self->param('photos_dir') . $q->param('i')); my $newimage; if (defined($q->param('l'))) { $newimage = resize_image(150, $image); } elsif (defined($q->param('m'))) { $newimage = resize_image(100, $image); } elsif (defined($q->param('s'))) { $newimage = resize_image(50, $image); } elsif (defined($q->param('f'))) { my $type = substr($q->param('i'), length($q->param('i')) - 3); if ($type eq 'jpg') { $self->header_props({-type=>'image/jpeg'}); return $image->jpeg; } elsif ($type eq 'png' || $type eq 'gif') { $self->header_props({-type=>'image/png'}); return $image->png; } } else { my ($width, $height) = $image->getBounds(); my $html = $self->load_tmpl('photos_single.tmpl', global_vars => 1); $html->param(script_name => $self->param('script_name')); $html->param(title => 'My Photo Gallery'); $html->param(filename => $q->param('i')); $html->param(alt => $q->param('i')); $html->param(width => $width); $html->param(height => $height); my $file = $self->param('photos_dir') . substr($q->param('i'), 0, length($q->param('i')) - 4); open (FH, "<$file.txt"); local $/ = undef; $html->param(caption => ); close FH; return $html->output; } $self->header_props({-type=>'image/png'}); return $newimage->png; } sub resize_image { my $newsize = shift; my $image = shift; my ($width, $height) = $image->getBounds(); my $image2 = new GD::Image($newsize, $newsize); $image2->transparent($image2->colorAllocate(0,0,0)); if ($width > $height) { $image2->copyResized($image, 0, int((($newsize - int(($height * $newsize / $width) + 0.5)) / 2) + 0.5), 0, 0, $newsize, int(($height * $newsize / $width) + 0.5), $width, $height); } elsif ($width < $height) { $image2->copyResized($image, int((($newsize - int(($width * $newsize / $height) + 0.5)) / 2) + 0.5), 0, 0, 0, int(($width * $newsize / $height) + 0.5), $newsize, $width, $height); } else { $image2->copyResized($image, 0, 0, 0, 0, $newsize, $newsize, $width, $height); } return $image2; } 1;