blib/lib/Apache/ImgIndex.pm untested

filter: Statementstmt 0.0% 0 / 246 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 407 CC 47 SCAR 46.8
CC
47
Cov
0%
handler
210.0
showImgThumbs
210.0
showImgDetail
72.0
CRAP 108.2
1 #!/usr/bin/perl
2 # Apache::ImgIndex
3  
4 #
5 # James Pavlick, 2000
6 # mod_perl module for indexing directories of images
7 #
8  
9 =head1 NAME
10  
11 Apache::ImgIndex
12  
13 =head1 SYNOPSIS
14  
15 Add the following to your httpd.conf and restart. Then point your browser to
16 http://yoursite/photos
17  
18  
19 <Location /photos>
20 AllowOverride None
21 #Options -Indexes -Includes -FollowSymLinks
22 Order allow,deny
23 Allow from all
24  
25 SetHandler perl-script
26 PerlHandler Apache::ImgIndex
27 PerlSetVar Rows 10
28 PerlSetVar Cols 5
29 PerlSetVar Thumb-size 50x20
30 PerlSetVar Show-names 1
31 PerlSetVar Hide-dirs 1
32 </Location>
33  
34 Make sure /photos contains the full size images that you want to display.
35  
36 =head1 DESCRIPTION
37  
38 I<Apache::ImgIndex> is a simple mod_perl application for displaying photos.
39 I<Apache::ImgIndex> will automatically build thumbnails of the images. You
40 can also rotate and scale the images from the web interface.
41  
42 =head1 CAVEATS
43  
44 =over 2
45  
46 =item B<*> Lots of work still to do
47  
48 =cut
49  
50  
51  
52 package Apache::ImgIndex;
53 use strict;
54 use Apache2;
55 #use Apache::compat;
56 use Apache::RequestRec;
57 use Apache::RequestIO;
58 use Apache::Const qw(:common HTTP_OK);
59 use Apache::Log;
60 use APR::Const qw(:filetype);
61 use APR::Finfo;
62 use Image::Magick ();
63 use DirHandle ();
64 use FileHandle ();
65 use File::Basename qw(fileparse);
66  
67 use vars qw(%gOptions @gOutput $gOutputStarted $VERSION);
68  
69 $VERSION = 0.02;
70  
71 %gOptions = (
72 'thumb-size' => '100x75', # set thumbnail size
73 'force' => 0, # always rebuild thumbnails
74 'rows' => 5, # rows to display
75 'cols' => 4, # columns to display
76 'show-names' => 0, # show thumbnail names
77 'hide-dirs' => 0, # hide the photo directories list
78 'filter' => '.*', # display only directories that match
79 );
80  
81  
82  
83 sub handler {
84 my($r) = shift;
85  
86 # store output in this array
87 @gOutput = ();
88  
89 $gOutputStarted = 0;
90  
91 # set config values
92 my $val = '';
93 $gOptions{'thumb-size'} = $val if($val = $r->dir_config('Thumb-size'));
94 $gOptions{'force'} = $val if($val = $r->dir_config('Force'));
95 $gOptions{'rows'} = $val if($val = $r->dir_config('Rows'));
96 $gOptions{'cols'} = $val if($val = $r->dir_config('Cols'));
97 $gOptions{'show-names'} = $val if($val = $r->dir_config('Show-names'));
98 $gOptions{'hide-dirs'} = $val if($val = $r->dir_config('Hide-dirs'));
99 $gOptions{'doc-root'} = $r->document_root . $r->location;
100 $gOptions{'base-url'} = $r->location;
101 $gOptions{'filter'} = $val if($val = $r->dir_config('Filter'));
102  
103  
104 # do processing for a directory of images
105 if($r->finfo->filetype == DIR) {
106  
107 if($r->args =~ /name=/) {
108 # show image detail html page
109 showImgDetail($r);
110  
111 } else {
112 # show the thumbnail index html page
113 my ($name, $path, $ext) = fileparse($r->filename, qr{\.\w*$});
114 mkdir("$path/.thumbs") unless(-d "$path/.thumbs");
115 showImgThumbs($r);
116 }
117 }
118  
119 # do processing on the image file itself
120 if($r->filename =~ /-thumb/) {
121 showThumbFile($r);
122 }
123  
124 if($r->finfo->filetype == REG) {
125 if($r->content_type =~ m:^image/: && $r->args) {
126 # rotate the image
127 processImg($r);
128  
129 } else {
130 #just pass the image down the chain
131 return DECLINED;
132 }
133 }
134  
135 $r->print(@gOutput);
136  
137 return OK;
138 }
139  
140  
141 # showImgThumbs
142 #
143 # list the contents of a directory as an HTML thumbnail index
144 #
145 sub showImgThumbs {
146 my($r) = shift;
147  
148 my($dir) = $r->filename;
149 my($baseUri) = $r->uri;
150 my(%in) = map { split('=', $_) } (split('&', $r->args));
151  
152 my($perpage) = $gOptions{'rows'} * $gOptions{'cols'};
153 my $root = $gOptions{'doc-root'};
154 my($start) = $in{'start'} || 0;
155  
156 # remove the base directory from the file path
157 (my $dirName = $dir) =~ s/^$root//;
158  
159 my($dh) = DirHandle->new($dir) ||
160 $r->log_error("Can't open directory '$dir': $?");
161  
162 my(@filelist) = ();
163 while( defined($_ = $dh->read) ) {
164 next unless(/^(.+)\.(jpg|gif|png)$/i);
165 next if(/^\./);
166 # next if($1 =~ /thumb/);
167  
168 push(@filelist, $_);
169 }
170  
171 $dh->close;
172  
173  
174 # Output page to client
175 $r->content_type('text/html');
176 output(
177 "<HTML>\n",
178 "<HEAD>\n",
179 " <TITLE>Image Index: $dirName</TITLE>\n",
180 "</HEAD>\n",
181 qq|<BODY bgcolor="#DDDDDD">\n\n|,
182 qq|<CENTER>\n|,
183 );
184  
185 output(
186 qq|<TABLE width="170" align="left" border="0">\n|,
187 qq| <TR><TD><b>Image Directories:</b></TD></TR>\n|,
188 listDirectory($dir),
189 qq|</TABLE>\n\n|,
190 ) unless($gOptions{'hide-dirs'});
191  
192 output(
193 qq|<TABLE border=0 bgcolor="#CCCCCC">\n|,
194 qq| <TR><TD>\n|,
195 qq| <TABLE border=0 cellpadding=0 cellspacing=0 width="100%">\n|,
196 qq| <TR><TD align="center" bgcolor="#AAAAAA"><font size="+1"><b>$dirName</font></b></TD></TR>\n|,
197 qq| </TABLE>\n\n|,
198 );
199  
200  
201 my($w, $h) = split('x', $gOptions{'thumb-size'});
202  
203 ### Thumbnail image table
204 output(qq| <TABLE align="center" bgcolor="#CCCCCC" border="1" width="|, ($w + 25 )* $gOptions{'cols'}, qq|">\n|);
205  
206 if(@filelist) {
207 my($counter) = 0;
208 my @sorted = (sort @filelist);
209  
210 for(my $x = $start; $x < @sorted; $x++) {
211 $sorted[$x] =~ /^(.+)\.(jpg|gif|png)$/i;
212 my($name, $ext) = ($1, $2);
213  
214 # set the display name
215 my $displayName = '';
216 if($gOptions{'show-names'}) {
217 ($displayName = $name) =~ s/[_\-]/\s/g;
218 $displayName = substr($displayName, 0, 22) . "..."
219 if(length $displayName >= 22);
220 }
221  
222 my($thumbName) = "${name}-thumb.$ext";
223
224 output(" <TR>\n") if( $counter == 0 || ($counter % $gOptions{'cols'}) == 0 );
225 output(
226 " ",
227 qq| <TD align="center">|,
228 qq|<A href="./?start=$start&name=$name.$ext">|,
229 qq|<IMG src="./.thumbs/$thumbName" hspace="15" height="$h" width="$w"></A>|,
230 qq|<BR>|,
231 $displayName,
232 qq|</TD>\n|,
233 );
234  
235 output(" </TR>\n") if( (($counter + 1) % $gOptions{'cols'}) == 0 );
236  
237 last if($counter == ($perpage - 1));
238 $counter++;
239 }
240  
241 } else {
242 output( qq|<tr><td align="center"><font size="+2"><b>No Images Available</b></font</td></tr>\n|);
243 }
244  
245 output(qq| </TABLE>\n\n|,);
246  
247  
248  
249 ### Navigation bar table
250 output(
251 qq| <TABLE bgcolor="#AAAAAA" cellpadding=0 cellspacing=0 border=0 width="100%">\n|,
252 qq| <TR>\n|
253 );
254  
255 my($index) = 0;
256 # previous page
257 if($start - $perpage >= 0) {
258 $index = $start - $perpage;
259 output(qq| <TD width=100 align="center"><A href="$baseUri?start=$index">[previous]</A></TD>\n|);
260  
261 } else {
262 output(qq| <TD width=100>&nbsp;</TD>\n|);
263 }
264  
265  
266  
267 # page indexes
268 if(@filelist) {
269 output(
270 qq| <TD align="center" valign="bottom">\n|,
271 qq| <FORM>\n|,
272 qq| <SELECT onChange="document.location='$baseUri?start=' + this.options[this.selectedIndex].value">\n|
273 );
274
275 for(my $y = 0; $y <= int((@filelist - 1)/$perpage); $y++) {
276 $index = $perpage * $y;
277 my $pagenum = $y + 1;
278  
279 ($index == $start) ?
280 output(qq| <option value="$index" selected>page $pagenum\n|):
281 output(qq| <option value="$index">page $pagenum\n|);
282 }
283  
284 output(
285 qq| </SELECT><BR>\n|,
286 qq| <font size="-1"><i>Total: </i>|, scalar @filelist, qq| images</font>\n|,
287 qq| </FORM>\n|,
288 qq| </TD>\n|
289 );
290 }
291
292 # next page
293 if($start + $perpage < $#filelist) {
294 $index = $start + $perpage;
295 output(qq| <TD width=100 align="center"><A href="$baseUri?start=$index">[next]</A></TD>\n|);
296  
297 } else {
298 output(qq| <TD width=100>&nbsp;</TD>\n|);
299 }
300  
301  
302 output(
303 " </TR>\n",
304 " </TABLE>\n",
305 " </TD></TR>\n",
306 "</TABLE>\n",
307 "</CENTER>\n",
308 "</BODY>\n",
309 "</HTML>\n"
310 );
311  
312 }
313  
314  
315  
316 # showThumbFile
317 #
318 #
319 sub showThumbFile {
320 my $r = shift || return;
321  
322 my %mimeType =
323 (jpg => 'image/jpeg', gif => 'image/gif', png => 'image/png');
324 my ($name, $path, $ext) = fileparse($r->filename, qr{\.\w*$});
325 my($w, $h) = split('x', $gOptions{'thumb-size'});
326  
327 $name =~ s/^\.//g;
328 $path =~ s/\/$//g;
329 $ext =~ s/^\.//g;
330
331 (my $imgName = $name) =~ s/-thumb$//i;
332 my($thumbName) = "$name.$ext";
333 my($Img) = Image::Magick->new();
334 my($tw, $th) = (0, 0);
335  
336 if(-f "$path/$thumbName") {
337 ($tw, $th) = $Img->Ping("$path/$thumbName");
338 }
339  
340 # build the thumbnail if it doesn't exist
341 if( ($w != $tw && $h != $th) || $gOptions{'force'} ) {
342 $Img->Read("$path/../$imgName.$ext");
343 $Img->Resize(geometry=>"$gOptions{'thumb-size'}");
344 $Img->Write("$path/$thumbName");
345 }
346  
347 my $Fh = FileHandle->new();
348 $Fh->open("$path/$thumbName") || die("Can't open image file $path/$thumbName");
349  
350 $r->content_type($mimeType{lc $ext});
351 output(<$Fh>);
352  
353 undef $Img;
354 $Fh->close();
355 }
356  
357  
358  
359 # showImgDetail
360 #
361 #
362 #
363 sub showImgDetail {
364 my($r) = shift;
365  
366 my(%in) = map { my($key, $val) = split('=', $_); $key => $val }
367 (split('&', $r->args));
368 my($baseUri) = $r->uri;
369  
370 # only rotate up to 360 degrees
371 if($in{'rot'} >= 360) { $in{'rot'} -= 360 };
372  
373 # only allow scaling to 25% and 200% of image size
374 if($in{'scale'} < -75) { $in{'scale'} = -75 };
375 if($in{'scale'} > 100) { $in{'scale'} = 100 };
376  
377 my $scale = $in{'scale'} + 100;
378  
379 my $rotText = ($in{'rot'}) ? "(Rotated: $in{'rot'}&deg;)" : '';
380 my $scaleText = ($in{'scale'}) ? "(Scaled: $scale%)" : '';
381
382 $r->content_type("text/html");
383 output(
384 "<HTML>\n",
385 "<HEAD>\n",
386 " <TITLE>Image: $in{'name'} $rotText $scaleText</TITLE>\n",
387 "</HEAD>\n",
388 qq|<BODY bgcolor="#ffffff">\n\n|,
389 "<CENTER>\n",
390 qq|<TABLE border=1>\n|,
391 qq| <TR><TD>&nbsp;</TD><TD align="center"><A href="$baseUri?start=$in{'start'}"><B>Image Index</B></A></TD><TD>&nbsp;</TR>\n|,
392 qq| <TR><TD align="center"><A href="$baseUri?start=$in{'start'}&name=$in{'name'}&rot=|,
393 $in{'rot'} + 270, qq|&scale=$in{'scale'}"><B>270</B></A></TD>|,
394 qq|<TD align="center"><A href="$baseUri?start=$in{'start'}&name=$in{'name'}&rot=$in{'rot'}&scale=|, $in{'scale'} - 25, qq|">-</A> Zoom <A href="$baseUri?start=$in{'start'}&name=$in{'name'}&rot=$in{'rot'}&scale=|, $in{'scale'} + 25, qq|">+</A></TD>|,
395 qq|<TD align="center"><A href="$baseUri?start=$in{'start'}&name=$in{'name'}&rot=|,
396 $in{'rot'} + 90, qq|&scale=$in{'scale'}"><B>90</B></A></TD></TR>\n|,
397 qq| <TR><TD>&nbsp;</TD><TD align="center"><A href="$baseUri?start=$in{'start'}&name=$in{'name'}&rot=|,
398 $in{'rot'} + 180, qq|&scale=$in{'scale'}"><B>180</B></A></TD><TD>&nbsp;</TD></TR>\n|,
399 "</TABLE>\n",
400 qq|<TABLE border=1 width="100%">\n|,
401 );
402  
403  
404 output(qq| <TR><TD align="center"><IMG src="$baseUri/$in{'name'}?|);
405 output(qq|rot=$in{'rot'}|) if($in{'rot'});
406 output(qq|&scale=$in{'scale'}|) if($in{'scale'});
407 output(qq|" border=0></TD></TR>\n|);
408  
409 output(
410 "</TABLE>\n",
411 "</CENTER>\n",
412 "</BODY>\n",
413 "</HTML>\n",
414 );
415 }
416  
417  
418 # processImg
419 #
420 #
421 #
422 sub processImg {
423 my($r) = shift;
424  
425 my(%in) = map { my($key, $val) = split('=', $_); $key => $val } (split('&', $r->args));
426  
427 my($imgfile) = $r->filename;
428 my ($name, $path, $ext) = fileparse($imgfile, qr{\..*});
429  
430 $path =~ s/\/$//g;
431 $ext =~ s/^\.//g;
432  
433 # only rotate up to 360 degrees
434 if($in{'rot'} >= 360) { $in{'rot'} -= 360 };
435  
436  
437 # only allow scaling to 25% and 200% of image size
438 if($in{'scale'} < -75) { $in{'scale'} = -75 };
439 if($in{'scale'} > 100) { $in{'scale'} = 100 };
440  
441 if(%in) {
442 my($tmpfile) = "/tmp/$name." . time . ".$$.$ext";
443
444 my $scale = $in{'scale'} + 100;
445
446 my($Img) = Image::Magick->new;
447 $Img->Read($imgfile);
448 $Img->Rotate(degrees=>$in{'rot'}) if($in{'rot'});
449 $Img->Scale(geometry=>"${scale}%x${scale}%") if($in{'scale'});
450 $Img->Write("$tmpfile");
451  
452 my($fh) = FileHandle->new("$tmpfile");
453  
454 unless($fh) {
455 $r->log_error("Couldn't open file '$tmpfile'");
456 return SERVER_ERROR;
457 }
458
459 local $/;
460 output(<$fh>);
461 $fh->close;
462  
463 unlink $tmpfile;
464  
465 } else {
466 return DECLINED;
467 }
468  
469 }
470  
471  
472 # dirIndex
473 #
474 #
475 sub dirIndex {
476 my($dirlist) = shift;
477 my($dir) = shift;
478  
479 my($filter) = $gOptions{'filter'};
480 my($dh) = DirHandle->new($dir);
481 my(@contents) = $dh->read;
482 $dh->close;
483  
484 foreach my $item (sort @contents) {
485 # skip directories containing .private files
486 next if(-e "$dir/$item/.private");
487  
488 # skip files/directories starting with .
489 next if($item =~ /^\.+/);
490  
491 # skip directories that aren't in the "filter" list
492 next unless("$dir/$item" =~ /$filter/);
493  
494 # Add the item to the the directory list if it's a directory
495 next unless(-d "$dir/$item");
496 $dirlist->{"$dir/$item"}{'name'} = $item;
497 }
498 }
499  
500  
501  
502 # listDirectory
503 #
504 #
505 sub listDirectory {
506 my($dir) = shift;
507  
508 my($dirlist) = {};
509 my($root) = $gOptions{'doc-root'} || '';
510 my($baseurl) = $gOptions{'base-url'} || '';
511 my($subdir) = $dir;
512 my $retval = '';
513  
514 # build a list of directories under the root
515 while($subdir =~ /^$root/) {
516 $subdir =~ s/\/+$//;
517 dirIndex($dirlist, $subdir);
518 $subdir = (fileparse($subdir))[1];
519 }
520  
521
522 # translate directory names into urls
523 foreach my $item (sort keys %$dirlist) {
524 my($dirname) = $dirlist->{$item}{'name'};
525  
526 (my $url = $item) =~ s/^$root//;
527 $url =~ s/^\/+//;
528 $url =~ s/\/+$//;
529 $url = join('/', $baseurl, $url);
530 my $tab = '--' x ( split('/', $url) - 3);
531 $retval .= qq| <tr><td>$tab<a href="$url/">$dirname</a><td></tr>\n|;
532 }
533  
534 $retval;
535 }
536  
537  
538  
539 sub output { push(@gOutput, @_); }
540  
541  
542  
543 1;