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 <options>
17 or: gal scan <options> <files and directories>
18 or: gal scan <options> --update
21 --add Keep existing images and add new ones
29 'update!' => \$update,
30 ) or die "Try gal scan --help\n";
33 my $gal = UCW::Gallery->load_config;
34 my $orig_prefix = $gal->get('OrigDir');
35 $orig_prefix =~ m{/$} or $orig_prefix .= '/';
39 print "Loading previous gallery.list\n";
40 my $pg = $gal->read_list('gallery.list') or die "Unable to load gallery.list\n";
45 push @source, { file => $in };
47 opendir D, $in or die "Cannot scan directory $in: $!\n";
49 while (my $e = readdir D) {
50 my $f = File::Spec->canonpath(File::Spec->catfile($in, $e));
51 if ($f =~ m{\.(jpe?g|png)$}i) {
56 push @source, map { { file => $_ } } sort @p;
58 die "$in is neither file nor directory\n";
62 binmode STDIN, ':utf8';
63 @source = @{$gal->read_list_fh(\*STDIN)};
66 my $hashes = UCW::Gallery::Hashes->new($gal);
68 print "Scanning photos\n";
70 foreach my $src (@source) {
71 my $name = $src->{file};
73 # Try to relativize to OrigDir
74 if (substr($name, 0, length $orig_prefix) eq $orig_prefix) {
75 $src->{file} = $name = substr($name, length $orig_prefix);
80 my $path = File::Spec->rel2abs($name, $gal->get('OrigDir'));
81 -f $path or die "Cannot find $path\n";
83 $src->{id} = $hashes->hash_image($path);
84 print " id=", $src->{id};
86 if (!defined $src->{orientation} || $src->{orientation} eq '-') {
87 my $e = new Image::EXIF($path);
88 my $i = $e->get_all_info();
90 print "EXIF error: ", $e->error, "\n";
91 $src->{orientation} = '.';
93 # print STDERR Dumper($i), "\n";
94 my $o = $i->{'image'}->{'Image Orientation'} || "Top, Left-Hand";
95 if ($o eq "Top, Left-Hand") { $o = "."; }
96 elsif ($o eq "Right-Hand, Top") { $o = "r"; }
97 elsif ($o eq "Left-Hand, Bottom") { $o = "l"; }
98 elsif ($o eq "Bottom, Right-Hand") { $o = "u"; }
100 print "Unrecognized orientation: $o\n";
103 $src->{orientation} = $o;
106 print " ori=", $src->{orientation};
108 defined $src->{xf} or $src->{xf} = $gal->get('ScanDefaultTransform');
109 print " xfrm=", $src->{xf};
111 $src->{title} //= '';
117 my $old = $gal->read_list('gallery.list');
119 print "Updating gallery.list\n";
120 my %new_by_id = map { $_->{id} => $_ } @images;
124 my $i = $new_by_id{$id};
127 print "\t$id: kept\n";
130 print "\t$id: removed\n";
133 print "\t$id: kept\n";
135 delete $new_by_id{$id};
138 for my $i (@images) {
140 $new_by_id{$id} or next;
141 print "\t$id: new\n";
148 $gal->write_list('gallery.list', \@images);
149 print "Written gallery.list (", (scalar @images), " items)\n";