Apache-ImgIndex
view release on metacpan or search on metacpan
ImgIndex.pm view on Meta::CPAN
return OK;
}
# showImgThumbs
#
# list the contents of a directory as an HTML thumbnail index
#
sub showImgThumbs {
my($r) = shift;
my($dir) = $r->filename;
my($baseUri) = $r->uri;
my(%in) = map { split('=', $_) } (split('&', $r->args));
my($perpage) = $gOptions{'rows'} * $gOptions{'cols'};
my $root = $gOptions{'doc-root'};
my($start) = $in{'start'} || 0;
# remove the base directory from the file path
(my $dirName = $dir) =~ s/^$root//;
my($dh) = DirHandle->new($dir) ||
$r->log_error("Can't open directory '$dir': $?");
my(@filelist) = ();
while( defined($_ = $dh->read) ) {
next unless(/^(.+)\.(jpg|gif|png)$/i);
next if(/^\./);
# next if($1 =~ /thumb/);
push(@filelist, $_);
}
$dh->close;
# Output page to client
$r->content_type('text/html');
output(
"<HTML>\n",
"<HEAD>\n",
" <TITLE>Image Index: $dirName</TITLE>\n",
"</HEAD>\n",
qq|<BODY bgcolor="#DDDDDD">\n\n|,
qq|<CENTER>\n|,
);
output(
qq|<TABLE width="170" align="left" border="0">\n|,
qq| <TR><TD><b>Image Directories:</b></TD></TR>\n|,
listDirectory($dir),
qq|</TABLE>\n\n|,
) unless($gOptions{'hide-dirs'});
output(
qq|<TABLE border=0 bgcolor="#CCCCCC">\n|,
qq| <TR><TD>\n|,
qq| <TABLE border=0 cellpadding=0 cellspacing=0 width="100%">\n|,
qq| <TR><TD align="center" bgcolor="#AAAAAA"><font size="+1"><b>$dirName</font></b></TD></TR>\n|,
qq| </TABLE>\n\n|,
);
my($w, $h) = split('x', $gOptions{'thumb-size'});
### Thumbnail image table
output(qq| <TABLE align="center" bgcolor="#CCCCCC" border="1" width="|, ($w + 25 )* $gOptions{'cols'}, qq|">\n|);
if(@filelist) {
my($counter) = 0;
my @sorted = (sort @filelist);
for(my $x = $start; $x < @sorted; $x++) {
$sorted[$x] =~ /^(.+)\.(jpg|gif|png)$/i;
my($name, $ext) = ($1, $2);
# set the display name
my $displayName = '';
if($gOptions{'show-names'}) {
($displayName = $name) =~ s/[_\-]/\s/g;
$displayName = substr($displayName, 0, 22) . "..."
if(length $displayName >= 22);
}
my($thumbName) = "${name}-thumb.$ext";
output(" <TR>\n") if( $counter == 0 || ($counter % $gOptions{'cols'}) == 0 );
output(
" ",
qq| <TD align="center">|,
qq|<A href="./?start=$start&name=$name.$ext">|,
qq|<IMG src="./.thumbs/$thumbName" hspace="15" height="$h" width="$w"></A>|,
qq|<BR>|,
$displayName,
qq|</TD>\n|,
);
output(" </TR>\n") if( (($counter + 1) % $gOptions{'cols'}) == 0 );
last if($counter == ($perpage - 1));
$counter++;
}
} else {
output( qq|<tr><td align="center"><font size="+2"><b>No Images Available</b></font</td></tr>\n|);
}
output(qq| </TABLE>\n\n|,);
### Navigation bar table
output(
qq| <TABLE bgcolor="#AAAAAA" cellpadding=0 cellspacing=0 border=0 width="100%">\n|,
qq| <TR>\n|
);
my($index) = 0;
# previous page
if($start - $perpage >= 0) {
$index = $start - $perpage;
output(qq| <TD width=100 align="center"><A href="$baseUri?start=$index">[previous]</A></TD>\n|);
} else {
output(qq| <TD width=100> </TD>\n|);
}
# page indexes
if(@filelist) {
output(
qq| <TD align="center" valign="bottom">\n|,
qq| <FORM>\n|,
qq| <SELECT onChange="document.location='$baseUri?start=' + this.options[this.selectedIndex].value">\n|
);
for(my $y = 0; $y <= int((@filelist - 1)/$perpage); $y++) {
$index = $perpage * $y;
my $pagenum = $y + 1;
($index == $start) ?
output(qq| <option value="$index" selected>page $pagenum\n|):
output(qq| <option value="$index">page $pagenum\n|);
}
output(
qq| </SELECT><BR>\n|,
qq| <font size="-1"><i>Total: </i>|, scalar @filelist, qq| images</font>\n|,
qq| </FORM>\n|,
qq| </TD>\n|
);
}
# next page
if($start + $perpage < $#filelist) {
$index = $start + $perpage;
output(qq| <TD width=100 align="center"><A href="$baseUri?start=$index">[next]</A></TD>\n|);
} else {
output(qq| <TD width=100> </TD>\n|);
}
output(
" </TR>\n",
" </TABLE>\n",
" </TD></TR>\n",
"</TABLE>\n",
"</CENTER>\n",
"</BODY>\n",
"</HTML>\n"
);
}
# showThumbFile
#
#
sub showThumbFile {
my $r = shift || return;
my %mimeType =
(jpg => 'image/jpeg', gif => 'image/gif', png => 'image/png');
my ($name, $path, $ext) = fileparse($r->filename, qr{\.\w*$});
my($w, $h) = split('x', $gOptions{'thumb-size'});
$name =~ s/^\.//g;
$path =~ s/\/$//g;
$ext =~ s/^\.//g;
(my $imgName = $name) =~ s/-thumb$//i;
my($thumbName) = "$name.$ext";
my($Img) = Image::Magick->new();
my($tw, $th) = (0, 0);
if(-f "$path/$thumbName") {
($tw, $th) = $Img->Ping("$path/$thumbName");
}
# build the thumbnail if it doesn't exist
if( ($w != $tw && $h != $th) || $gOptions{'force'} ) {
$Img->Read("$path/../$imgName.$ext");
$Img->Resize(geometry=>"$gOptions{'thumb-size'}");
$Img->Write("$path/$thumbName");
}
( run in 0.933 second using v1.01-cache-2.11-cpan-5735350b133 )