Main »

Picst Thumbnails


#!/usr/bin/perl -wT

# name this code picst.cgi
# dump into a directory with a bunch of pictures.
# when browsed to, will dynamically generate urls for all the pictures in the dir.
# pictures open in a new browser window when url is clicked on.

# thumbnails can be generated and placed in a thumbs/ dir by using the script
#    thumbn.pl
#    find pics/ \( -iname *.jpg -o -iname *.jpeg -o -iname *.png -o -iname *.gif -o -iname *.tif -o -iname *.tiff \) -exec ./thumbn.pl {} \;

my $urlVar = "dir=";
$urlVar = $ENV{'QUERY_STRING'} if $ENV{'QUERY_STRING'};
my ($key, $dir) = split /=/,$urlVar;
$dir =~ s/^\/|\/$//g;
my @subDir = split /\//,$dir;
my $urlRoot = $ENV{SCRIPT_NAME};
$urlRoot =~ s/\?.+$/\//;  # remove the get values in the url
my $cgiName = $urlRoot;
$cgiName =~ s/\/$//;
$cgiName =~ s/^.+\///g; # want clean cgi name 
my $navBar = "  <a href=\"$cgiName\">home</a>" if($dir ne ''); # want url for 'home' if not at home.
$navBar = "home" if($dir eq '');  # no url if at home.
$navBar .= genNavBar(@subDir); # get dir branch and set up nav bar.

print "Content-type: text/html\n\n";
print <<EOP;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Expires" CONTENT="Mon, 23 Sep 1996 01:21:00 GMT">
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>my pics</TITLE>
<STYLE TYPE="TEXT/CSS">
<!--
p	{
	color: #999999;
	font-size:8pt;
        font-family:arial;
        font-style:normal;
        font-weight:normal;
        background-color: black;
 	}
body	{
	margin-left:50px;
	color:#999999;
	font-size:8pt;
        font-family:arial;
        font-style:normal;
        font-weight:normal;
	background-color: black; 
	}
A:link
	{
	color:#dddd00;
	font-size:small;
	font-family:arial;
	font-style:normal;
	font-weight:normal;
	text-decoration:none;
	}
A:active
	{
	color:#ffff00;
	font-size:small;
	font-family:arial;
	font-style:normal;
	font-weight:normal;
	}
A:visited
	{
	color:#999900;
	font-size:small;
	font-family:arial;
	font-style:normal;
	font-weight:normal;
	text-decoration:none;
	}
A:hover
	{
	color:#ffff00;
	font-size:small;
	font-family:arial;
	font-style:normal;
	font-weight:normal;
	}
-->
</STYLE>
</HEAD>
<BODY>
EOP

  #print "<br>debug:: urlRoot=$urlRoot; cgiName=$cgiName; urlVar=$urlVar; key=$key; dir=$dir <br>\n";
print "<br>&nbsp; $navBar <br><br>\n";

opendir(DIR,"./$dir")  || die "NO SUCH Directory: Images";
foreach my $file ( sort readdir(DIR) ){ # print the dir at top of the listings.
        if($file =~ /^(\.||\.\.||index.html|RCS|.*\.cgi|.*\.swp|thumbs|.*\.pl)$/i){next;}
        my $fullDir = "$dir/$file/";
        $fullDir =~ s/^\/|\/$//g; # remove leading or trailing slashes
        if(-d $fullDir){print " &nbsp;&nbsp; <a href=\"$cgiName\?dir=$fullDir\">dir-&gt; $file</a><br>\n";}
} 
closedir(DIR);
print "<br><br>\n";

opendir(DIR,"./$dir") || die "NO SUCH Directory: ./$dir";
foreach my $file ( sort readdir(DIR) ){
        if($file =~ /^(\.||\.\.||index.html|RCS|.*\.cgi|.*\.swp|thumbs|.*\.pl)$/i){next;}
	my $fullDir = "$dir/$file/";
	my $thumb = $file;
	$thumb =~ s/(.+?)\.(.+)$/$1_thumb\.$2/;  # need to looks for filenames the _thumb.xxx
	$fullDir =~ s/^\/|\/$//g; # remove leading or trailing slashes
	my $fileSize = sprintf("%.2f",(-s $fullDir) / 1000000); # want file size in Mbytes.
	if(-d $fullDir){next;} # dirs we print at top of file list to screen ie already printed by this time.
        elsif(-e "./$dir/thumbs/$thumb"){print " &nbsp;&nbsp; <a href=\"$fullDir\" target=\"_blank\">$file</a> &nbsp;&nbsp; <a href=\"$fullDir\" target=\"_blank\"> <img src=\"./$dir/thumbs/$thumb\" border=\"0\"></a>";} # with thumbnail
	else{print " &nbsp;&nbsp; <a href=\"$fullDir\" target=\"_blank\">$file</a>";} # no thumbnail.
	print " &nbsp;&nbsp; ( $fileSize mb ) <br>\n"; # this only prints on files.
   }
closedir(DIR);

print "<br></body></html>\n";

sub genNavBar { # generate the nav bar to get back
	my @dirs = @_;  # sent cginame, dir array
	my ($cnt,$updir,$navString);
	my $arrayCnt = scalar @dirs;
	foreach my $dir (@dirs){
		$cnt++;
		$updir .= "$dir/";
		if($cnt < $arrayCnt){$navString .= qq' / <a href="?dir=$updir">$dir</a>';}
		else{$navString .= " / $dir"};
	}
return($navString);
} # end of sub genNavBar.


top level subjects:

Page last modified on December 28, 2009, at 04:50 AM

^