2 # UCW Gallery: Scan images and generate image list
3 # (c) 2004--2012 Martin Mares <mj@ucw.cz>
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";
52 my $gal = UCW::Gallery->load_config;
53 my $orig_prefix = $gal->get('OrigDir');
54 $orig_prefix =~ m{/$} or $orig_prefix .= '/';
56 print "Scanning photos\n";
58 foreach my $f (@files) {
60 if (substr($rel_name, 0, length $orig_prefix) eq $orig_prefix) {
61 $rel_name = substr($rel_name, length $orig_prefix);
63 print "\t$rel_name: ";
65 my $sha = Digest::SHA->new(1);
66 $sha->addfile($f) or die "Cannot hash $f\n";
67 my $id = substr($sha->hexdigest, 0, 16);
70 my $e = new Image::EXIF($f);
71 my $i = $e->get_all_info();
72 if ($e->error) { print "EXIF error: ", $e->error, "\n"; }
74 # print STDERR Dumper($i), "\n";
75 my $o = $i->{'image'}->{'Image Orientation'} || "Top, Left-Hand";
76 if ($o eq "Top, Left-Hand") { $o = "."; }
77 elsif ($o eq "Right-Hand, Top") { $o = "r"; }
78 elsif ($o eq "Left-Hand, Bottom") { $o = "l"; }
79 elsif ($o eq "Bottom, Right-Hand") { $o = "d"; }
81 print "Unrecognized orientation: $o\n";
88 xfrm => $gal->get('ScanDefaultTransform'),
95 my $old = $gal->read_list('gallery.list');
97 print "Updating gallery.list\n";
98 my %old_by_id = map { $_->{id} => $_ } @$old;
101 my $o = $old_by_id{$id};
103 print "\t$id: updated\n";
104 $i->{orientation} = $o->{orientation};
105 $i->{xfrm} = $o->{xfrm};
106 $i->{title} = $o->{title};
108 print "\t$id: new\n";
110 delete $old_by_id{$id};
112 for my $id (keys %old_by_id) {
113 print "\t$id: removed\n";
117 $gal->write_list('gallery.list', \@images);
118 print "Written gallery.list (", (scalar @images), " items)\n";