2 # UCW Gallery: Scan images and generate image list
3 # (c) 2004--2012 Martin Mares <mj@ucw.cz>
10 use UCW::Gallery qw(%CF);
16 if (@ARGV && $ARGV[0] eq '--help') {
19 or: $0 <files and directories>
29 opendir D, $in or die "Cannot scan directory $in: $!\n";
31 while (my $e = readdir D) {
32 my $f = File::Spec->canonpath(File::Spec->catfile($in, $e));
33 if ($f =~ m{\.(jpe?g|png)$}i) {
40 die "$in is neither file nor directory\n";
50 UCW::Gallery::LoadConfig;
51 my $orig_prefix = $CF{'OrigDir'};
52 $orig_prefix =~ m{/$} or $orig_prefix .= '/';
54 print "Scanning photos\n";
56 foreach my $f (@files) {
58 if (substr($rel_name, 0, length $orig_prefix) eq $orig_prefix) {
59 $rel_name = substr($rel_name, length $orig_prefix);
61 print "\t$rel_name: ";
63 my $sha = Digest::SHA->new(1);
64 $sha->addfile($f) or die "Cannot hash $f\n";
65 my $id = substr($sha->hexdigest, 0, 16);
68 my $e = new Image::EXIF($f);
69 my $i = $e->get_all_info();
70 if ($e->error) { print "EXIF error: ", $e->error, "\n"; }
72 # print STDERR Dumper($i), "\n";
73 my $o = $i->{'image'}->{'Image Orientation'} || "Top, Left-Hand";
74 if ($o eq "Top, Left-Hand") { $o = "."; }
75 elsif ($o eq "Right-Hand, Top") { $o = "r"; }
76 elsif ($o eq "Left-Hand, Bottom") { $o = "l"; }
77 elsif ($o eq "Bottom, Right-Hand") { $o = "d"; }
79 print "Unrecognized orientation: $o\n";
86 xfrm => $CF{'ScanDefaultTransform'},
93 my $old = UCW::Gallery::ReadList('gallery.list');
95 print "Updating gallery.list\n";
96 my %old_by_id = map { $_->{id} => $_ } @$old;
99 my $o = $old_by_id{$id};
101 print "\t$id: updated\n";
102 $i->{orientation} = $o->{orientation};
103 $i->{xfrm} = $o->{xfrm};
104 $i->{title} = $o->{title};
106 print "\t$id: new\n";
108 delete $old_by_id{$id};
110 for my $id (keys %old_by_id) {
111 print "\t$id: removed\n";
115 UCW::Gallery::WriteList('gallery.list', \@images);
116 print "Written gallery.list (", (scalar @images), " items)\n";