#!/usr/local/bin/perl5 -w use strict; die "usage: $0 \n" unless @ARGV; my $lighterPrefix = ".l-"; my $verbose = 0; my $errcutoff = 2; #< main loop, figure out what to do my $errors=0; foreach my $arg (@ARGV) { if (-d $arg) { $errors += &makethumbs($arg); } elsif ($arg eq "-" or -f $arg) { open(IN, $arg) or do { warn "cannot read [$arg]: $!\n"; $errors++; next;}; while () { chomp; next if /^\#/ or !/\S/; next if /\b(RCS|Thumbs)\b/; $errors += &makethumbs($_); last if $errors > $errcutoff; } close IN; } else { warn "no such file or directory [$arg]\n"; $errors++; } if ($errors > $errcutoff) { warn "stopping after $errors errors.\n"; last; } } #> #< make thumbs for a single directory sub makethumbs { my $dir = shift; warn "no such directory [$dir]\n" and return unless -d $dir; #warn "cannot write directory [$dir]\n" and return unless -w _; my $tdir = qq($dir/Thumbs); warn "cannot make directory [$tdir]: $!\n" and return unless -e $tdir or mkdir($tdir, 0777); opendir(TDIR, $tdir) or do {warn "cannot read Thumbs directory [$tdir]: $!\n"; return;}; my @ts = readdir TDIR; close TDIR; my %ts=(); foreach my $t (@ts) { next if $t =~ /^$lighterPrefix/o or -d qq($tdir/$t); $ts{$t} = 0; } opendir(DIR, $dir) or do {warn "cannot read directory [$dir]: $!\n"; return;}; my @es = readdir DIR; close DIR; my @ps = grep { !/^\./ and /\S\S+\.(gif|jpg|png)$/i } @es; print STDERR "$dir: ".scalar(@ps)." images\n" if $verbose; my $cnt=0; return 0 unless @ps; foreach my $p (@ps) { $ts{$p} = 1; my $pic = qq($dir/$p); my $thumb = qq($tdir/$p); if (not -e $thumb or -M _ > -M $pic) { $cnt += &convert($pic, $thumb); } } foreach my $t (keys %ts) { # these are unused pre-existing thumbnails. remove them. print "removing unused thumbs for [$t]\n" unless $ts{$t}; &remove($dir, $tdir, $t) unless $ts{$t}; } return ($cnt / @ps > .1); } #> sub sh { my $cmd = shift; #print STDERR $cmd.$/; system $cmd and do { warn "convert error [$cmd]: $!$@\n"; return undef; }; return 1; } #< remove the leavings of a removed image sub remove { my ($dir, $tdir, $name) = @_; sh qq(/bin/rm -f "$dir/$lighterPrefix$name") or return 1; sh qq(/bin/rm -f "$tdir/$lighterPrefix$name") or return 1; sh qq(/bin/rm -f "$tdir/$name") or return 1; return 0; } #> #< convert an image to a thumbnail sub convert { my ($pic, $thumb) = @_; print STDERR "mkthumbs $pic\n"; sh qq(chmod a+r "$pic") or return 1; sh qq(/usr/bin/convert -resize x300 "$pic" "$thumb") or return 1; my $lthumb = $thumb; $lthumb =~ s|/([^/]+)$|/$lighterPrefix$1|; sh qq(/usr/bin/convert -gamma 2.2 "$thumb" "$lthumb") or return 1; my $bytes = -s $pic; my $kbytes = $bytes / 1024; my $mbytes = $kbytes / 1024; my $target = 3; # megabytes if ($target < $mbytes) { my $percent = int($target * 100 / $mbytes); # roughly my $display_mbytes = sprintf('%3.2f',$mbytes); warn " ...shrinking it to $percent% of ${display_mbytes}Mb\n"; # shrink the original -- space is getting out of hand. my $b = $pic; $b =~ s|.*/||; sh qq(/usr/bin/convert -resize $percent% "$pic" "./smaller-$$-$b"); sh qq(/bin/mv "./smaller-$$-$b" "$pic"); } my $lpic = $pic; $lpic =~ s|/([^/]+)$|/$lighterPrefix$1|; sh qq(/usr/bin/convert -gamma 2.2 "$pic" "$lpic") or return 1; return 0; } #>