2 # UCW Gallery: Scan images and generate image list
3 # (c) 2004--2015 Martin Mares <mj@ucw.cz>
9 use UCW::Gallery::Hashes;
14 if (@ARGV && $ARGV[0] eq '--help') {
16 Usage: cat list | gal scan
17 or: gal scan <files and directories>
24 'update!' => \$update,
25 ) or die "Try gal scan --help\n";
28 my $gal = UCW::Gallery->load_config;
29 my $orig_prefix = $gal->get('OrigDir');
30 $orig_prefix =~ m{/$} or $orig_prefix .= '/';
34 print "Loading previous gallery.list\n";
35 my $pg = $gal->read_list('gallery.list') or die "Unable to load gallery.list\n";
40 push @source, { file => $in };
42 opendir D, $in or die "Cannot scan directory $in: $!\n";
44 while (my $e = readdir D) {
45 my $f = File::Spec->canonpath(File::Spec->catfile($in, $e));
46 if ($f =~ m{\.(jpe?g|png)$}i) {
51 push @source, map { { file => $_ } } sort @p;
53 die "$in is neither file nor directory\n";
59 push @source, { file => $_ };
63 my $hashes = UCW::Gallery::Hashes->new($gal);
65 print "Scanning photos\n";
67 foreach my $src (@source) {
68 my $name = $src->{file};
70 # Try to relativize to OrigDir
71 if (substr($name, 0, length $orig_prefix) eq $orig_prefix) {
72 $src->{file} = $name = substr($name, length $orig_prefix);
77 my $path = File::Spec->rel2abs($name, $gal->get('OrigDir'));
78 -f $path or die "Cannot find $path\n";
80 $src->{id} = $hashes->hash_image($path);
81 print " id=", $src->{id};
83 if (!defined $src->{orientation} || $src->{orientation} eq '-') {
84 my $e = new Image::EXIF($path);
85 my $i = $e->get_all_info();
87 print "EXIF error: ", $e->error, "\n";
88 $src->{orientation} = '.';
90 # print STDERR Dumper($i), "\n";
91 my $o = $i->{'image'}->{'Image Orientation'} || "Top, Left-Hand";
92 if ($o eq "Top, Left-Hand") { $o = "."; }
93 elsif ($o eq "Right-Hand, Top") { $o = "r"; }
94 elsif ($o eq "Left-Hand, Bottom") { $o = "l"; }
95 elsif ($o eq "Bottom, Right-Hand") { $o = "u"; }
97 print "Unrecognized orientation: $o\n";
100 $src->{orientation} = $o;
103 print " ori=", $src->{orientation};
105 defined $src->{xfrm} or $src->{xfrm} = $gal->get('ScanDefaultTransform');
106 print " xfrm=", $src->{xfrm};
108 $src->{title} //= '';
114 my $old = $gal->read_list('gallery.list');
116 print "Updating gallery.list\n";
117 my %new_by_id = map { $_->{id} => $_ } @images;
121 my $i = $new_by_id{$id};
123 print "\t$id: removed\n";
126 print "\t$id: updated\n";
128 delete $new_by_id{$id};
130 for my $i (@images) {
132 $new_by_id{$id} or next;
133 print "\t$id: new\n";
140 $gal->write_list('gallery.list', \@images);
141 print "Written gallery.list (", (scalar @images), " items)\n";