in reply to simple image gallery generated by server image directory
in thread simple column/row counter

I thought this code was really neat because I was working on something similar. So I took it and made some changes, that hopefully improve it.

I also broke it up into two files because of some of the other things I'm going to add for my specific project.

I would be interested to know if anybody else has suggestions on how to make it better, just to help me learn.

Also, even though it works for me, I really don't understand what the line $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; is doing. If anybody can help shed some light on that, I'd appreciate it.
#!/usr/bin/perl -wT # photo_display.pl # script to select a category and display images within that directory # sends data to photo_display2.pl use CGI qw( :standard ); $startdir="/www/htdocs/photos"; # all images are in subdirectories o +f this directory push(@dirs,$startdir); foreach $dir (@dirs) { opendir(DIR,"$dir") || die ( "Error opening : $!" ); @list = grep { !/^\.\.?$/ } readdir(DIR); closedir(DIR); foreach $item (@list) { $fullname = $dir."/".$item; # add the directory name to th +e file name if (-d $fullname) { push(@dirs,$fullname); # print "$item<br>"; } } } print( header() ); print( start_html( -title => "Image Gallery", -dtd => '-//W3C//DTD HTML 4.01 Transitional//EN', -meta=>{'description'=>'Image Gallery', -created=>'20020926'}, -style =>{'src'=>'http:/photos/css/photos.css'}, -background =>'http:/images/flatwhit.gif' ) ); print '<h1>Image Gallery</h1>'; print '<hr />'; parse(); print <<FORM; <form action=/cgi-bin/photos/photo_display2.pl method=post> <select name=pic> <option value=$FORM{'pic'} selected>$FORM{'pic'} FORM @sortdirs = sort @dirs; foreach $thisdir (@sortdirs) { my $short = $thisdir; $short =~ s/\/www\/htdocs\/photos\///; # display only subdirectory +as option in dropdown print "<option value=$thisdir>$short</option>"; } print '<input type="submit" value="View Images"></form>'; print ( end_html() ); sub parse { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } }


#!/usr/bin/perl -wT # photo_display2.pl # display images within a directory # receives directory information from photo_display.pl use CGI qw( :standard ); print( header() ); print( start_html( -title => "USGS Photos", -dtd => '-//W3C//DTD HTML 4.01 Transitional//EN', -meta=>{'description'=>'Photo Gallery', -created=>'20020926'}, -style =>{'src'=>'http:/photos/css/photos.css'}, -background =>'http:/images/flatwhit.gif' ) ); print '<h1>Image Gallery</h1>'; print '<hr />'; print '<p>Click on an image to view a larger version.</p>'; my $dir = param( "pic" ); opendir(DIR,$dir) || die ("Error opening : $!" ); #directory contains text files that should not be used @piclist = grep { !/^\.\.?$/ and /\.jpg$|\.gif$|\.png$|\.JPG$|\.bmp$/ +} readdir(DIR); @sortpiclist = sort @piclist; #images will display in alphanumeric or +deR closedir(DIR); print "<table cellpadding=10 cellspacing=0 width=100%>"; while (@sortpiclist) { my @row_entries= splice (@sortpiclist, 0, 4); print "<tr>"; for (@row_entries) { print "<td>"; $dir =~ s/\/www\/htdocs//; print qq| <a href=$dir/$_> <img src=$dir/$_ border="0" width="180"></a><br>$_<p> |; print "</td>"; } print "</tr>"; } print ( end_html() );