#!/usr/bin/perl -w # # GALADMIN -- Gallery administrator # # Html generator and image converter # (c) 2002, 2003, 2004 Milos Kapoun, Pavel Ruzicka # my $Version="1.0.5"; # global variables my $indexname="index.html"; # Index file name my $directoryname="source"; # Source directory name my $commentfile="comment.txt"; # Name of comment file my $ThnWidthL=128; # Landscape thumbnail width my $ThnHeightL=96; # Landscape thumbnail height my $ThnWidthP=72; # Portrait thumbnail width my $ThnHeightP=96; # Portrait thumbnail height my $ImgWidthL=640; # Landscape image width my $ImgHeightL=480; # Landscape image height my $ImgWidthP=360; # Portrait image width my $ImgHeightP=480; # Portrait image height 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 File comment.txt: "gallery","My gallery name" "description","My gallery description" "PICT0884.JPG","Image name","l" # rotate 90 left "PICT0885.JPG","Image name","r" # rotate 90 right "PICT0886.JPG","Image name","" Examples: galadmin.pl -pi -- convert image sizes and generate html pages galadmin.pl -gr -- generate recursive index of all galleries EOF ; } # css file $css = < EOF ; sub GetName{ my($file)= @_; if (exists$Rotate{$file}) { return("${ImgWidthP}x${ImgHeightP}"); } else { return("${ImgWidthL}x${ImgHeightL}"); } } sub GetClass{ my($file)= @_; if (exists$Rotate{$file}) { return("p"); } else { return("l"); } } ################################ # 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 {$b cmp $a} @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 = (); %Rotate = (); while ($line = ) { chomp($line); if ($line =~ /^"(.*)","(.*)"$/) { $Comment{$1}=$2; } if ($line =~ /^"(.*)","(.*)","(.*)"$/) { $Comment{$1}=$2; my $xx = $3; my $nn = $1; if ($xx =~ /^[rl]$/) { $Rotate{$nn}=$xx; } } } close (IC); } else { print "$Dir[$i]->{Dir}->{path}/$commentfile not found, generating...\n"; %Comment = (); %Rotate = (); if (open(OC,">$Dir[$i]->{Dir}->{path}/$commentfile")) { print OC "\"gallery\",\"\"\n"; print OC "\"description\",\"\"\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 = "";} if (exists($Comment{'description'})){$GalDesc = "
\n$Comment{'description'}\n
\n";} else {$GalDesc = "";} print OL $index; print OL <$Dir[$i]->{Dir}->{path}

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

EOF ; } # css file # if (defined($opt_p)) { $file = ">".$DestDir."/".$Dir[$i]->{Dir}->{path}."/$directoryname/galadmin.css"; open(OCSS,$file) or die "Can't open $file\n"; print OCSS $css; close(OCSS); } @files =(); @files = sort(keys(%{$Dir[$i]->{File}})); for($w = 0; $w < @files; $w++) { $y = $files[$w]; #print "$w - $y\n"; # image converting if ($opt_i) { if (!( ( exists($Rotate{$y}) || (-e "$DestDir/$Dir[$i]->{Dir}->{path}/$directoryname/${ImgWidthL}x${ImgHeightL}_$y")) && (!(exists($Rotate{$y})) || (-e "$DestDir/$Dir[$i]->{Dir}->{path}/$directoryname/${ImgWidthP}x${ImgHeightP}_$y")) && (-e "$DestDir/$Dir[$i]->{Dir}->{path}/$directoryname/thn_$y") && !(defined($opt_f)) )) { print "Converting image: $Dir[$i]->{Dir}->{path}/$y\n"; if (exists($Rotate{$y}) && ($Rotate{$y} eq "r")) { # convert system("convert -rotate 90 -geometry ${ImgWidthP}x${ImgHeightP} -quality 80 $Dir[$i]->{Dir}->{path}/$y $DestDir/$Dir[$i]->{Dir}->{path}/$directoryname/${ImgWidthP}x${ImgHeightP}_$y >/dev/null 2>&1"); # thumbnail system("convert -rotate 90 -geometry ${ThnWidthP}x${ThnHeightP} -quality 80 $Dir[$i]->{Dir}->{path}/$y $DestDir/$Dir[$i]->{Dir}->{path}/$directoryname/thn_$y >/dev/null 2>&1"); } else { if (exists($Rotate{$y}) && ($Rotate{$y} eq "l")) { # convert system("convert -rotate -90 -geometry ${ImgWidthP}x${ImgHeightP} -quality 80 $Dir[$i]->{Dir}->{path}/$y $DestDir/$Dir[$i]->{Dir}->{path}/$directoryname/${ImgWidthP}x${ImgHeightP}_$y >/dev/null 2>&1"); # thumbnail system("convert -rotate -90 -geometry ${ThnWidthP}x${ThnHeightP} -quality 80 $Dir[$i]->{Dir}->{path}/$y $DestDir/$Dir[$i]->{Dir}->{path}/$directoryname/thn_$y >/dev/null 2>&1"); } else { # normal without rotation # convert system("convert -geometry ${ImgWidthL}x${ImgHeightL} -quality 80 $Dir[$i]->{Dir}->{path}/$y $DestDir/$Dir[$i]->{Dir}->{path}/$directoryname/${ImgWidthL}x${ImgHeightL}_$y >/dev/null 2>&1"); # thumbnail system("convert -geometry ${ThnWidthL}x${ThnHeightL} -quality 80 $Dir[$i]->{Dir}->{path}/$y $DestDir/$Dir[$i]->{Dir}->{path}/$directoryname/thn_$y >/dev/null 2>&1"); } } } else { print "Image ".GetName($y)."_$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 EOF ; # Prev image my $prev_html = ""; my $next_html = ""; if ($w > 0) { my $prev_img; $prev_img = $files[$w - 1]; $prev_nam = $prev_img; $prev_nam =~ s/\.[Jj][Pp][Gg]//; $prev_html .= "<<"; } # Next image if ($w < (@files - 1)) { my $next_img; $next_img = $files[$w + 1]; $next_nam = $next_img; $next_nam =~ s/\.[Jj][Pp][Gg]//; $next_html .= ">>"; } my $class = GetClass($y); my $sna = GetName($y); print OE < $prev_html INDEX $next_html
$PicComm - $y Original image
$y
EOF ; if (open (IE,"exiftags $Dir[$i]->{Dir}->{path}/$y |" )) { print OE "
\n"; print OE "\n"; print OE "\n"; } } print OE "<\/table>\n"; print OE "<\/div>\n"; 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 $GalDesc; 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