#!/usr/bin/perl #KIG is an image gallery generator. #Run kig from the directory where you want kig to generate all the #html files etc. #The arguments specify: #- The image directory of which you want to make html pages #recursively. #- set 1 if you specify a relative path(recommended) of # w.r.t the current directory else set to 0 for absolute paths use POSIX; if(@ARGV<2){ die "Usage: kig [UPDIR]\n KIG is an image gallery generator. Run kig from the directory where you want kig to generate all the html files etc. The arguments specify: - The image directory of which you want to make html pages recursively. - set 1 if you specify a relative path(recommended) of w.r.t the current directory else set to 0 for absolute paths\n" } ($imagedir, $RELATIVE, $UPDIR)=@ARGV; &createGal($imagedir, $UPDIR); sub createGal{ my ($imageDIR, $upDIR) = @_; ################################ #To handle spaces in directories(some problems with <$imageDIR/*> #if $imageDIR has space my @imagefiles = <$imageDIR/*>; my $oldDIRtemp=qx("pwd"); chomp($oldDIRtemp); chdir $imageDIR; my @imagefiles = <*>;#gives only file names without path for(my $i=0; $i<@imagefiles; $i++){ $imagefiles[$i]="$imageDIR/$imagefiles[$i]";#to make file names with path } chdir $oldDIRtemp; ############################### my @imageDIRsplit=split("/", $imageDIR); my $imageDIRname=$imageDIRsplit[@imageDIRsplit - 1]; open(OUTPUTHTML, ">$imageDIRname.html") or die "Can't open $imageDIRname.html $!\n"; #my $OUTPUTHTML_LOCAL=OUTPUTHTML; my $outputhtmltext=qq(Kwatra Image Gallery ); print OUTPUTHTML $outputhtmltext; if($upDIR){ $outputhtmltext=qq(
UP

); print OUTPUTHTML $outputhtmltext; } my @filelist; my $filecount=0; my @dirlist; my $dircount=0; my $file; foreach $file (@imagefiles){ #Sees all the files in the image DIR. if(-d $file){ #File is a Directory. $dirlist[$dircount]=$file; $dircount++; print "Directory: $file\n"; #print OUTPUTHTML "Directory: $file\n"; }else{ $filelist[$filecount]=$file; $filecount++; print "File: $file\n"; #print $OUTPUTHTML_LOCAL "File: $file\n"; #print OUTPUTHTML "File: $file\n"; } } foreach $dir (@dirlist){ my @dirsplit=split("/", $dir); my $dirname=$dirsplit[@dirsplit - 1]; my $basefilehtmltext; $basefilehtmltext=qq(
$dirname

); print OUTPUTHTML $basefilehtmltext; } $outputhtmltext=qq(
); print OUTPUTHTML $outputhtmltext; $filecount=0; foreach $file (@filelist){ ######################### my @imagefilesplit=split("/", $file); my $imagefilename=$imagefilesplit[@imagefilesplit- 1]; my $thumb_height=120; my $thumb_width=&calcScaledWidth($file, $thumb_height); my $thumb_file="thumb_$imagefilename"; # my $resize_cmd="convert $file -resize $thumb_width"."x$thumb_height $thumb_file"; # print "$resize_cmd\n"; # qx($resize_cmd); &createThumb($file, $thumb_width, $thumb_height, $thumb_file); ######################### $filecount++; my $nextfileno, $prevfileno, $prevcomment, $nextcomment;#$prevcomment, $nextcomment not implemented if($filecount==@filelist){ $nextfileno=1; }else{ $nextfileno=$filecount+1; } if($filecount==1){ $prevfileno=@filelist; }else{ $prevfileno=$filecount-1; } my $comment; my $filename="$filecount.html"; my $totalfiles=@filelist; &insertHTML($filename, $file, $comment, $imageDIRname, $prevfileno, $nextfileno, $prevcomment, $nextcomment, $filecount, $totalfiles); my $basefilehtmltext; if(($filecount-1)%5){ $basefilehtmltext=qq( ); }else{ $basefilehtmltext=qq( ); } print OUTPUTHTML $basefilehtmltext; } $outputhtmltext=qq(







The page was created by KIG ); print OUTPUTHTML $outputhtmltext; my $dir; foreach $dir (@dirlist){ #Directory: Perform Recursively. my @dirsplit=split("/", $dir); my $dirname=$dirsplit[@dirsplit - 1]; unless(-e $dirname){ mkdir($dirname, 0777) || die "cannot mkdir $dirname $!"; }else{ print "warning: directory $dirname already exists\n"; } my $oldDIR=qx("pwd"); print "storing oldDir as $oldDIR\n"; chomp($oldDIR); chdir $dirname; my $newDIR=$dir; if($RELATIVE){ $newDIR="../$dir"; } print "recursively calling $newDIR\n"; &createGal($newDIR, $imageDIRname); print "moving to oldDir: $oldDIR\n"; chdir $oldDIR; } close OUTPUTHTML; } sub insertHTML{ my ($filename, $file, $comment, $imageDIRname, $prevfileno, $nextfileno, $prevcomment, $nextcomment, $filecount, $totalfiles) = @_; my ($height, $width)=&size($file); open(FILEHTML, ">$filename") or die "Can't open $filename $!\n"; my $filehtmltext=$filehtmltext.qq( Kwatra Image Gallery

$file



Index       << Prev    Next >>
); print FILEHTML $filehtmltext; $filehtmltext=qq(

$filecount/$totalfiles
); if($comment){ $filehtmltext=$filehtmltext.qq(
$comment
); } else{ $filehtmltext=$filehtmltext.qq( ); } $filehtmltext=$filehtmltext.qq(
Index       << Prev    Next >>



The page was created by KIG ); print FILEHTML $filehtmltext; close FILEHTML; } sub size{ my ($file)=@_; $file=~ s/ /\\ /g; #To handle spaces in files open(IDENTIFY, "identify -verbose $file|"); my $file_geometry; my @file_geometry_split; my @file_height_width; while($line=){ #print $line; if($line=~/Geometry/){ $file_geometry=$line; } } @file_geometry_split=split(":", $file_geometry); @file_height_width =split("x", $file_geometry_split[1]); my ($width, $height) = @file_height_width; $width=~ s/ //; return ($height, $width); } sub createThumbold{#NOT BEING USED my ($file, $thumb_width, $thumb_height, $thumb_file)=@_; $file=~ s/ /\\ /g; #To handle spaces in files $thumb_file=~ s/ /\\ /g; #To handle spaces in files my $resize_cmd="convert $file -resize $thumb_width"."x$thumb_height $thumb_file"; print "$resize_cmd\n"; qx($resize_cmd); } sub calcScaledWidth{ my ($file, $height_new)=@_; my ($height_orig, $width_orig)=&size($file); my $width_new=0; if($height_orig){ $width_new= (($height_new)*($width_orig))/($height_orig); } return (floor($width_new)); } sub createThumb{ my ($file, $thumb_width, $thumb_height, $thumb_file)=@_; $file=~ s/ /\\ /g; #To handle spaces in files $thumb_file=~ s/ /\\ /g; #To handle spaces in files unless(-e $thumb_file){ my $resize_cmd="convert $file -resize $thumb_width"."x$thumb_height $thumb_file"; print "$resize_cmd\n"; qx($resize_cmd); }else{ print "warning: thumbnail $thumb_file already exists\n"; } }