#!/usr/bin/perl -w # # GALADMIN -- Gallery administrator # # Html generator and image converter # (c) 2002, 2003 Milos Kapoun, Pavel Ruzicka # $Version="1.0.0"; # global variables $IndexName="index.html"; $DirectoryName="source"; $CommentFile="comment.txt"; $ThnWidth=128; $ThnHeight=96; $ImgWidth=800; $ImgHeight=600; use Getopt::Std; &getopts("d:rDpihg"); ################################ # Print help # ################################ sub PrintHelp { print < Arguments: -d - destination directory -D - delete -g - gallery html index generating -h - this help -i - images generating -p - html pages generating -r - recursive, do all directories in Examples: galadmin.pl -pi -- convert image sizes and generate html pages galadmin.pl -gr -- generate recursive index of all galleries EOF ; } ################################ # scan directory # ################################ sub rdir{ my($adr)= @_; my(@soubory,$path,$soubor,$cnDir); if (not opendir(ADR,$adr)) {print "Can't open directory $adr.\n";return;} @soubory = readdir(ADR); closedir(ADR); $cnDir = $nDir; $Dir[$cnDir]->{Dir}->{level} = $lDir; $Dir[$cnDir]->{Dir}->{jpg} = 0; $Dir[$cnDir]->{Dir}->{dir} = 0; $Dir[$cnDir]->{Dir}->{path} = $adr; foreach $soubor (sort @soubory) { if ($soubor =~ /^\./ ) {next;} $path=$adr ."/". $soubor; if (-d $path) { # print "$path \n" if (!defined($opt_r)) {next;} if ($soubor eq $DirectoryName) {next;} $nDir++; $lDir++; $Dir[$nDir]->{Dir}->{name} = $soubor; &rdir($path); if (($Dir[$cnDir+1]->{Dir}->{dir} == 1)||($Dir[$cnDir+1]->{Dir}->{jpg}==1)) {$Dir[$cnDir]->{Dir}->{dir} = 1;} $lDir--; } else { if ($soubor =~ /\.[Jj][Pp][Gg]$/) { $Dir[$cnDir]->{Dir}->{jpg} = 1; $Dir[$cnDir]->{File}->{$soubor} = 1; } } } } ################################ # main program # ################################ if (defined($opt_h)) { PrintHelp(); exit(0); } if (!defined($ARGV[0])) { PrintHelp(); exit(0); } else { if (-d $ARGV[0]) { $ARGV[0]=~s/\/$//; $SourceDir = $ARGV[0]; print "Processing \"$SourceDir\" directory.\n"; } else { print "File $ARGV[0] is not a directory."; exit(0); } } if (!defined($opt_d)) { print "Destination directory is same as source directory.\n"; $DestDir = "."; $FlDestDif = 0; } else { $DestDir = $opt_d; $FlDestDif = 1; print "Destination directory is: $DestDir.\n"; } print "Scanning directories for images:\n"; $nic = defined($opt_r); $nic = defined($opt_h); $nic = defined($opt_D); $nic = defined($opt_g); $nic = defined($Comment); $nic++; $nDir=0; # number of dirs $lDir=0; # dir level # $Dir - field of dir $Dir[$nDir]->{Dir}->{name} = $SourceDir; rdir($SourceDir); # print directory foreach $i (0..$nDir) { if ($Dir[$i]->{Dir}->{jpg}||$Dir[$i]->{Dir}->{dir}) { print substr(" ",0,($Dir[$i]->{Dir}->{level})*3)."`--$Dir[$i]->{Dir}->{name}\n"; foreach $y (sort(keys(%{$Dir[$i]->{File}}))) { print substr(" ",0,($Dir[$i]->{Dir}->{level})*3)." |--"."$y\n"; } } } # option list print "END of list.\n"; foreach $i (0..$nDir) { if ($Dir[$i]->{Dir}->{jpg}) { if (defined($opt_p)||defined($opt_i)) { print "\nProcessing directory:$DestDir/$Dir[$i]->{Dir}->{path}\n"; } # mk directory if (defined($opt_p)||defined($opt_i)) { if (!(-e "$DestDir/$Dir[$i]->{Dir}->{path}/$DirectoryName")) { if(system("mkdir $DestDir/$Dir[$i]->{Dir}->{path}/$DirectoryName")) { print "Directory $DestDir/$Dir[$i]->{Dir}->{path}/$DirectoryName created.\n"; } } else { print "Directory $DestDir/$Dir[$i]->{Dir}->{path}/$DirectoryName already exists.\n"; } } if(defined($opt_p)) { %line = (); # read comment file if (open(IC,"<$Dir[$i]->{Dir}->{path}/$CommentFile")) { while ($line = ) { chomp($line); if ($line =~ /^"(.*)","(.*)"$/) { $Comment{$1}=$2; } } close (IC); } else { print "$Dir[$i]->{Dir}->{path}/$CommentFile not found, generating...\n"; if (open(OC,">$Dir[$i]->{Dir}->{path}/$CommentFile")) { print OC "\"gallery\",\"\"\n"; foreach $y (sort(keys(%{$Dir[$i]->{File}}))) { print OC "\"$y\",\"\"\n"; } } close (OC); } } # open HTML list file $file = ">".$DestDir."/".$Dir[$i]->{Dir}->{path}."/".$IndexName; # print "$file"; # if (defined($opt_p)) { open(OL,$file) or die "$file not opened"; if (exists($Comment{gallery})){$GalComm = " - $Comment{gallery}";} else {$GalComm = "";} } if (defined($opt_p)) { print OL < $Dir[$i]->{Dir}->{path}

Gallery: $Dir[$i]->{Dir}->{path}$GalComm

generated by galadmin.pl $Version

EOF ; } foreach $y (sort(keys(%{$Dir[$i]->{File}}))) { # image converting if (defined($opt_i)) { print "Converting image: $Dir[$i]->{Dir}->{path}/$y\n"; # convert system("convert -geometry ${ImgWidth}x${ImgHeight} -quality 80 $Dir[$i]->{Dir}->{path}/$y $DestDir/$Dir[$i]->{Dir}->{path}/$DirectoryName/${ImgWidth}x${ImgHeight}_$y >/dev/null 2>&1"); # thumbnail system("convert -geometry ${ThnWidth}x${ThnHeight} -quality 80 $Dir[$i]->{Dir}->{path}/$y $DestDir/$Dir[$i]->{Dir}->{path}/$DirectoryName/thn_$y >/dev/null 2>&1"); } # img_html file $z = $y; $z =~ s/\.[Jj][Pp][Gg]//; if (defined($opt_p)) { print "Generating html for image: $Dir[$i]->{Dir}->{path}/$y\n"; if ((exists($Comment{$y}))&&($Comment{$y} ne "")){$PicComm = $Comment{$y};} else {$PicComm = "$z";} print OL "
\n\"$y\"\n", "

$PicComm

\n"; print OL "
\n"; open(OE,">$DestDir/$Dir[$i]->{Dir}->{path}/$DirectoryName/img_$z.html") or die "Can't write img_$z.html"; print OE < $z

$PicComm

generated by galadmin.pl $Version

$y EOF ; if (open (IE,"exiftags $Dir[$i]->{Dir}->{path}/$y |" )) { while ($line = ) { chomp($line); if ($line =~ /^(.*?):(.*)$/) { print OE "\n"; } } close (IE); } else { print "Program exiftags is not installed.\n"; } print OE < EOF ; close (OE); } } if (defined($opt_p)) { print OL "\n"; close (OL); } if(defined($opt_D)) { system("rm $DestDir/$Dir[$i]->{Dir}->{path}/$IndexName"); system("rm -r $DestDir/$Dir[$i]->{Dir}->{path}/$DirectoryName"); } } } if (defined($opt_g)) { $tree = "
    \n"; $slevel = 0; foreach $i (0..$nDir) { if ($Dir[$i]->{Dir}->{jpg}||$Dir[$i]->{Dir}->{dir}) { while ($Dir[$i]->{Dir}->{level} < $slevel) { $tree .= substr("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t",0,$Dir[$i]->{Dir}->{level}); $tree .= "
\n"; $slevel--; } while ($Dir[$i]->{Dir}->{level} > $slevel) { $tree =~ s/<\/li>\n$/\n/; $tree .= substr("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t",0,$slevel); $tree .= "
    \n"; $slevel++; } $tree .= substr("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t",0,$Dir[$i]->{Dir}->{level}); if ($Dir[$i]->{Dir}->{jpg}) { $GalComm = ""; if (open(IC,"<$Dir[$i]->{Dir}->{path}/$CommentFile")) { while ($line = ) { chomp($line); if ($line =~ /^"(.*)","(.*)"$/) { $Comment{$1}=$2; if (exists($Comment{gallery}) && ($Comment{gallery} ne "")){$GalComm = " - $Comment{gallery}";} else {$GalComm = "";} last; } } close (IC); } $path = $Dir[$i]->{Dir}->{path}; if ( -e "$path/$IndexName") { $file = "/$IndexName"; } else { $file = ""; } $path .= "$file"; $path =~ s/^$SourceDir//; $path =~ s/^\///; $tree .= "
  • $Dir[$i]->{Dir}->{name}$GalComm
  • \n"; } else { $tree .= "
  • $Dir[$i]->{Dir}->{name}
  • \n"; } } } while ($slevel > 0) { $tree .= "
\n"; $slevel-- ; } $tree .= "\n"; } # index.html if (defined($opt_g)) { print "Destination directory: $SourceDir\n"; open(OI,">".$SourceDir."/".$IndexName) or die "Can't open index file"; print OI < Gallery INDEX
Image name: $y Original image
$1:$2

Gallery INDEX

generated by galadmin.pl $Version

$tree EOF ; close (OI); } exit (0);