#!/usr/bin/perl -w # # GALADMIN -- Gallery administrator # # Html generator and image converter # (c) 2002, 2003 Milos Kapoun, Pavel Ruzicka # my $Version="1.0.3"; # global variables my $indexname="index.html"; my $directoryname="source"; my $commentfile="comment.txt"; my $ThnWidth=128; my $ThnHeight=96; my $ImgWidth=800; my $ImgHeight=600; use vars qw( $opt_d $opt_r $opt_D $opt_p $opt_i $opt_h $opt_g $opt_f); use Getopt::Std; &getopts("d:rDpihgf"); my @Dir; # field of Dirs my $nDir=0; # number of dirs my $lDir=0; # dir level ################################ # Print help # ################################ sub PrintHelp { print < Arguments: -d - destination directory -D - delete -g - gallery html index generating -h - this help -i - images generating, skip existing -f - force, overwrite existing images (only with -i) -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) { 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 # ################################ my $SourceDir; my $DestDir; if ($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 ($opt_d) { $DestDir = $opt_d; print "Destination directory is: $DestDir.\n"; } else { $DestDir = "."; print "Destination directory is same as source directory.\n"; } print "Scanning directories for images:\n"; # $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 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")) { %Comment = (); while ($line = ) { chomp($line); if ($line =~ /^"(.*)","(.*)"$/) { $Comment{$1}=$2; } } close (IC); } else { print "$Dir[$i]->{Dir}->{path}/$commentfile not found, generating...\n"; %Comment = (); 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 "Can't open $file\n"; if (exists($Comment{gallery})){$GalComm = " - $Comment{gallery}";} else {$GalComm = "";} print OL < $Dir[$i]->{Dir}->{path}

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

EOF ; } foreach $y (sort(keys(%{$Dir[$i]->{File}}))) { # image converting if ($opt_i) { if (!((-e "$DestDir/$Dir[$i]->{Dir}->{path}/$directoryname/${ImgWidth}x${ImgHeight}_$y") && (-e "$DestDir/$Dir[$i]->{Dir}->{path}/$directoryname/thn_$y") && !(defined($opt_f)) )) { 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"); } else { print "Image ${ImgWidth}x${ImgHeight}_$y and thn_$y already exists skipping.\n"; } } # img_html file $z = $y; $z =~ s/\.[Jj][Pp][Gg]//; if ($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

$y Original size

$y
EOF ; if (open (IE,"exiftags $Dir[$i]->{Dir}->{path}/$y |" )) { print OE "\n"; print OE "\n"; } } print OE "<\/table>"; close (IE); } else { print "Program exiftags not found.\n"; } print OE "
\n"; print OE "Generated by galadmin.pl<\/a> $Version\n"; print OE "<\/div>\n"; print OE "<\/body>\n<\/html>\n"; close (OE); } } if ($opt_p) { print OL "
\n"; print OL "Generated by galadmin.pl<\/a> $Version\n"; print OL "<\/div>\n"; print OL "<\/body>\n<\/html>\n"; close (OL); } if ($opt_D) { system("rm $DestDir/$Dir[$i]->{Dir}->{path}/$indexname"); system("rm -r $DestDir/$Dir[$i]->{Dir}->{path}/$directoryname"); } } } my $tree; my $slevel; if ($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 .= "\n"; } # index.html if ($opt_g) { print "Destination directory: $SourceDir\n"; open(OI,">".$SourceDir."/".$indexname) or die "Can't open index file"; print OI < Gallery INDEX

Gallery INDEX

$tree EOF ; close (OI); } exit (0);
EXIF Tags:<\/th><\/tr>\n"; while ($line = ) { chomp($line); if ($line =~ /^(.*?):(.*)$/) { print OE "
$1:$2