#!perl -w
# ===========================================================================================
# NAME: genimgindex.pl (see earlier simpe imgindex.pl)
# AIM: Given a BASE folder, seek all IMAGE files, and build
# an 'index' table of images, as HTML ...
# Commands: in-folder [-out out-file]
# 2018-08-25 - Included BMP image files...
# 2018-06-01 - Generate HTML5 template
# 20/04/2016 - Add -a to add text under image
# 06/03/2014 - Add -t \n";
$full_html .= " ";
$full_html .= get_YYYYMMDD(time());
$full_html .= ": ";
$full_html .= "This is a list of $img_cnt images";
if ($date_sort) {
$full_html .= ", in approximate date order.";
}
$full_html .= "\n";
if ($add_lnk) {
$full_html .= " Click on the image to load the full image";
$full_html .= " in a new window" if ($add_blank);
$full_html .= ".\n";
}
if ($link2file) {
$full_html .= " The link below each image links to the most recent page where this \n";
$full_html .= " image is featured, if found.\n";
}
$full_html .= "
\n";
$targ = "target=\"_blank\"" if ($add_blank);
$irel = '.';
if ($fix_relat && (length($in_folder))) {
$irel = get_relative_path_test( $in_folder, $target_dir );
# $irel = get_relative_path_test( $def_in, $def_out );
prt( "Using relative path of [$irel] ...\n" );
}
if ($date_sort && ($img_cnt > 1)) {
prt("Do 'stat' each, get date/time, and sort by date...\n");
my @files_to_search = ();
my @arr = ();
my @arrs = ();
for ($i = 0; $i < $img_cnt; $i++) {
$fil = $imglist[$i];
$sb = stat($fil); # get the file date, time, size, etc
push(@arr, [$sb->mtime, $fil]);
}
@arrs = sort mycmp_decend @arr;
@imglist = (); # reset list, and put in DATE order
for ($i = 0; $i < $img_cnt; $i++) {
$fil = $arrs[$i][1];
$sf = substr($fil, length($in_folder) + 1 );
push(@imglist, $fil);
}
if ($link2file) {
@files_to_search = get_htm_file_list( $def_out );
}
}
# start HTML text collection
prt("Start HTML text collection...\n");
$full_html = get_html_bgn();
$full_html .= get_html_body_bgn();
if (!$bare_table) {
$full_html .= "\n \n";
$wrap = 0;
while ($wrap < $add_cols) {
$wrap++;
$full_html .= " \n";
}
my $order = '';
$wrap = 0; # restart WRAP
for ($i = 0; $i < $img_cnt; $i++) {
###last if ($i > 1);
$i2 = $i + 1;
$fil = $imglist[$i];
$isblank = ($fil eq 'blank') ? 1 : 0;
$sf = $fil; # this is the image SOURCE file, with or without an input folder...
if (!$got_order) {
$sf = substr($fil, length($in_folder) + 1 ) if (length($in_folder));
}
($nm, $dir, $ext) = fileparse( $sf, qr/\.[^.]*/ );
$sb = stat($fil) if (!$isblank); # get the file date, time, size, etc
$isz = '';
$ratio = 1;
$iwd = 1;
$iht = 1;
$attr = '';
$const = '';
$src = $sf;
$relsrc = $irel.$sf;
$relhrf = $irel.$sf;
$relpath = get_relative_path( $cwdir, $dir );
$outsrc = $src;
$outsrc = $relsrc if ($fix_relat);
$imgsrc = $outsrc;
$thsrc = $imgsrc;
if (length($thumb_dir)) {
$tmp = $thumb_dir;
ut_fix_directory(\$tmp);
$tmp .= $nm;
if (-f $tmp.".jpg") {
$thsrc = $tmp.".jpg"
} elsif (-f $tmp.".png") {
$thsrc = $tmp.".png"
} else {
prg_exit(1,"ERROR: Thumb $tmp jpg nor png found! FIX ME \n");
}
}
$linksrc = $src;
$linksrc = $relhrf if ($fix_relat);
# exceptions....
if ($add_alt_link && length($alt_src_link)) {
if ($drop_thumb) {
$linksrc = drop_thumb_name($linksrc);
}
$outsrc = $alt_src_link . '/' . $linksrc;
}
#$src = dos_2_unix($fil) if ($dbg2);
$src = path_2_html($fil) if ($dbg2);
if (!$isblank) {
$isz = im_get_image_size($fil);
$iwd = im_get_image_width($isz);
$iht = im_get_image_height($isz);
$ratio = $iwd / $iht;
$imgSx = $iwd;
$imgSy = $iht;
}
if ($add_isz || $set_max) {
if ($set_max) {
if (($iwd > $targwid) || ($iht > $targwid)) {
if($ratio > 1) {
$imgSx = $targwid;
$imgSy = int($targwid / $ratio);
} else {
$imgSx = int($targwid * $ratio);
$imgSy = $targwid;
}
$attr = " width=\"$imgSx\"\n";
$attr .= " height=\"$imgSy\"";
$const = "".$imgSx."x".$imgSy;
}
}
}
$dir = "." if (length($dir) == 0);
$full_html .= "\n \n Name\n \n" if ($add_name && !$one_cell);
$full_html .= " \n Directory\n \n" if ($add_dir && !$one_cell);
$full_html .= " \n Image\n \n";
$full_html .= " \n Size\n \n" if ($add_isz && !$one_cell);
$full_html .= " \n Bytes\n \n" if ($add_siz && !$one_cell);
}
$full_html .= " \n" if ($wrap == 0);
if (!$one_cell) {
if ($add_name) {
$full_html .= " \n";
$wrap = 0;
$rcnt++;
if ( ($row_jump > 0) && ($rcnt >= $row_jump) && (($img_cnt - $i) > ($row_jump + 2)) ) {
$full_html .= get_row_jump($add_cols) if (!$bare_table && !$got_order);
$rcnt = 0;
}
$order .= "\n";
}
prt( "Image: [$sf] ".$iwd."x".$iht.", scaled ".$imgSx."x".$imgSy." ($fil) (irel=$irel)\n" ) if (VERB5());
}
# finish off the row, if required
if ($wrap) {
while ($wrap < $add_cols) {
$full_html .= " \n";
$full_html .= $sf;
$full_html .= "\n \n";
}
if ($add_dir) {
$full_html .= "\n";
##$full_html .= dos_2_unix($dir);
$full_html .= $relpath;
$full_html .= "\n \n";
}
}
$alttxt = "$src";
$alttxt .= " ".$iwd.'x'.$iht if ($set_max);
$alttxt .= " index $i2";
# ======================================================================================
# main IMAGE cell, class .ctr
# ===============================================================================
$full_html .= " \n";
# **********************************************************
if ($add_lnk && !$isblank) {
# lllllllllllllllllllllllllllllllllllllllllllllllll
$full_html .= " '.$lfile.'';
}
}
$lhtm .= " ($isz)" if ($add_imsz);
if ($add_file_name) {
if ($set_max) {
if ($imgSx >= $targwid) {
$full_html .= " $lhtm";
} else {
$full_html .= "$addbr $lhtm";
}
} else {
$full_html .= "$addbr $lhtm";
}
}
$full_html .= "\n";
}
$full_html .= "$addbr is: $isz\n" if ($add_isz && length($isz) && !$add_imsz);
$full_html .= "$addbr ct: $const\n" if ($add_isz && length($const));
$full_html .= "$addbr fs: ".get_nn($sb->size)."\n" if ($add_siz);
}
# **********************************************************
$full_html .= " \n";
# ======================================================================================
if (!$one_cell) {
if ($add_isz && length($isz)) {
$full_html .= "\n";
$full_html .= $isz;
$full_html .= $const if length($const);
$full_html .= "\n \n";
}
if ($add_siz) {
$full_html .= "\n";
$full_html .= get_nn($sb->size);
$full_html .= "\n \n";
}
}
$wrap++;
if ($wrap == $add_cols) {
$full_html .= " \n /n \n" if ($add_name && !$one_cell);
$full_html .= " \n /n \n" if ($add_dir && !$one_cell);
$full_html .= " \n no image\n \n";
$full_html .= " \n \n \n" if ($add_isz && !$one_cell);
$full_html .= " \n \n \n" if ($add_siz && !$one_cell);
$wrap++;
$order .= "blank ";
}
$full_html .= " \n";
$order .= "\n";
}
$full_html .= get_html_end_table();
if (!$bare_table) {
$full_html .= get_end_link();
if ($add_valid_stg) {
$full_html .= get_tidy_valid();
} else {
$full_html .= " \n";
}
if ($add_old_link) {
$full_html .= get_old_link();
}
}
$full_html .= " \n";
$full_html .= get_html_end();
if ($show_full) {
prt( "======================================================================\n" );
prt( "$full_html\n" );
prt( "======================================================================\n" );
}
prt("Image order:\n$order");
$txt = join("\n",@img_list)."\n";
rename_2_old_bak($list_file);
write2file($txt,$list_file);
prt("Image list written to $list_file...\n");
rename_2_old_bak($of);
# dump it to FILE
if (open OUTF, ">$of") {
print OUTF $full_html;
close OUTF;
prt( "Written HTML to $of ...\n" );
$outsrc = "copy $of .\n";
write2file($outsrc,$copy_bat);
prt("Written $copy_bat to update...\n");
} else {
prt( "ERROR: FAILED TO WRITE $of !!!\n" );
}
}
sub in_excluded_images {
my ($fil) = shift;
my ($nm, $dir) = fileparse($fil);
foreach my $f (@excluded_imgs) {
if ($nm eq $f) {
return 1;
}
}
return 0;
}
sub my_in_file {
my ($fil) = shift;
my ($nm, $dir, $ext) = fileparse( $fil, qr/\.[^.]*/ );
foreach my $e (@imgfiles) {
if (lc($e) eq lc($ext)) {
return 1;
}
}
return 0;
}
sub is_fp_folder {
my ($fil) = shift;
foreach my $fp (@fpfolders) {
if (lc($fp) eq lc($fil)) {
return 1;
}
}
return 0;
}
sub collect_image_files($) {
my $inf = shift;
my $cnt = 0;
prt( "Processing $inf folder ...\n" );
if ( opendir( DIR, $inf ) ) {
my @files = readdir(DIR);
closedir DIR;
$cnt = scalar @files;
prt("readdir returned $cnt items...\n");
foreach my $fl (@files) {
if (($fl eq '.') || ($fl eq '..') || is_fp_folder($fl) ) {
next;
}
my $ff = $inf . "\\" . $fl;
if (-d $ff) {
collect_image_files($ff) if ($recursive);
} else {
prt("Checking '$fl' ... ") if (VERB9());
if (my_in_file($fl) && !in_excluded_images($fl)) {
if (in_xclude_list($fl)) {
$xclude_cnt++;
prt("in exclude") if (VERB9());
} else {
push(@imglist, $ff);
prt("Added") if (VERB9());
}
} else {
prt("NOT image") if (VERB9());
}
prt("\n") if (VERB9());
}
}
} else {
prt( "WARNING: Can NOT open $inf ... $! ...\n" );
}
}
#sub get_nn { # perl nice number nicenum add commas
# my ($n) = shift;
# if (length($n) > 3) {
# my $mod = length($n) % 3;
# my $ret = (($mod > 0) ? substr( $n, 0, $mod ) : '');
# my $mx = int( length($n) / 3 );
# for (my $i = 0; $i < $mx; $i++ ) {
# if (($mod == 0) && ($i == 0)) {
# $ret .= substr( $n, ($mod+(3*$i)), 3 );
# } else {
# $ret .= ',' . substr( $n, ($mod+(3*$i)), 3 );
# }
# }
# return $ret;
# }
# return $n;
#}
sub dos_2_unix {
my ($du) = shift;
$du =~ s/\\/\//g;
return $du;
}
sub unix_2_dos {
my ($du) = shift;
$du =~ s/\//\\/g;
return $du;
}
sub path_2_html {
my ($pth) = shift;
$pth = dos_2_unix($pth);
###$pth =~ s/ /%20/g;
return $pth;
}
sub get_relative_path_test {
my ($target, $fromdir) = @_;
my ($colonpos, $path, $posval, $diffpos);
##my ($from, $to);
my ($tlen, $flen);
my ($tolen, $fromlen);
my ($cht, $chf);
my $retrel = "";
# only work with slash - convert DOS backslash to slash
$target = path_d2u($target);
$fromdir = path_d2u($fromdir);
# add '/' to target. if missing
if (substr($target, length($target)-1, 1) ne '/') {
$target .= '/';
}
# add '/' to fromdir. if missing
if (substr($fromdir, length($fromdir)-1, 1) ne '/') {
$fromdir .= '/';
}
# remove drives, if present
if ( ( $colonpos = index( $target, ":" ) ) != -1 ) {
$target = substr( $target, $colonpos+1 );
}
if ( ( $colonpos = index( $fromdir, ":" ) ) != -1 ) {
$fromdir = substr( $fromdir, $colonpos+1 );
}
# got the TO and FROM ...
#$to = $target;
#$from = $fromdir;
$tolen = length($target);
$fromlen = length($fromdir);
prt( "To [$target]($tolen),\nfrom [$fromdir]($fromlen) ...\n" ) if ($tdbg_rel);
$path = '';
$posval = 0;
$retrel = '';
# // Step through the paths until a difference is found (ignore slash differences)
# // or until the end of one is found
# while ( substr($from,$posval,1) && substr($to,$posval,1) ) {
while ( ($posval < $tolen) && ($posval < $fromlen) ) {
$chf = substr($fromdir,$posval,1);
$cht = substr($target,$posval,1);
if ( $chf eq $cht ) {
$posval++; # bump to next
} else {
prt( "First diff [$chf] ne [$cht] ...\n" ) if ($tdbg_rel);
last; # break;
}
}
##if ( !substr($from,$posval,1) ) {
if ( $posval >= $fromlen ) {
prt( "Ran out of from ...\n" ) if ($tdbg_rel);
}
##if ( !substr($to,$posval,1) ) {
if ( $posval >= $tolen ) {
prt( "Ran out of to ...\n" ) if ($tdbg_rel);
}
# // Save the position of the first difference
$diffpos = $posval;
prt( "First diff found at offset $posval ... ".substr($target,$posval)." ...\n" ) if ($tdbg_rel);
# // Check if the directories are the same or
# // the if target is in a subdirectory of the fromdir
if ( ( !substr($fromdir,$posval,1) ) &&
( substr($target,$posval,1) eq "/" || !substr($target,$posval,1) ) )
{
# // Build relative path
$diffpos = length($target);
if (($posval + 1) < $diffpos) {
$diffpos-- if ($diffpos);
if ($diffpos > $posval) {
$diffpos -= $posval;
} else {
$diffpos = 0;
}
###$retrel = substr( $target, $posval+1, length( $target ) );
prt( "Return substr of target, from ".($posval+1).", for $diffpos length ...\n" ) if ($tdbg_rel);
###$retrel = substr( $target, $posval+1, $diffpos );
$retrel = substr( $target, ($posval+1) );
} else {
prt( "posval+1 (".($posval+1).") greater than length $diffpos ...\n" ) if ($tdbg_rel);
}
} else {
# // find out how many "../"'s are necessary
# // Step through the fromdir path, checking for slashes
# // each slash encountered requires a "../"
#$posval++;
while ( substr($fromdir,$posval,1) ) {
prt( "Check for slash ... $posval in $fromdir\n" ) if ($tdbg_rel);
if ( substr($fromdir,$posval,1) eq "/" ) { # || ( substr($fromdir,$posval,1) eq "\\" ) ) {
prt( "Found a slash, add a '../' \n" ) if ($tdbg_rel);
$path .= "../";
}
$posval++;
}
prt( "Backed relative path = [$path] ...\n" ) if ($tdbg_rel);
# // Search backwards to find where the first common directory
# // as some letters in the first different directory names
# // may have been the same
$diffpos--;
while ( ( substr($target,$diffpos,1) ne "/" ) && substr($target,$diffpos,1) ) {
$diffpos--;
}
# // Build relative path to return
$retrel = $path . substr( $target, $diffpos+1, length( $target ) );
}
prt( "Returning [$retrel] ...\n" ) if ($tdbg_rel);
return $retrel;
}
###################################################
#### bit of the HTML file
sub get_html_bgn {
my $html_bgn = '';
my $html_bgn1 = <
$html_title
EOF
if ($bare_table) {
return " \n";
}
return $html_body_bgn;
}
sub html_bare_table {
my $html_bare_table = <