2 # UCW Gallery: Scan images and generate image list
3 # (c) 2004--2015 Martin Mares <mj@ucw.cz>
8 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 $e = Encode::decode('utf-8', $e);
51 my $f = File::Spec->canonpath(File::Spec->catfile($in, $e));
52 if ($f =~ m{\.(jpe?g|png)$}i) {
57 push @source, map { { file => $_ } } sort @p;
59 die "$in is neither file nor directory\n";
63 binmode STDIN, ':utf8';
64 @source = @{$gal->read_list_fh(\*STDIN)};
67 my $hashes = UCW::Gallery::Hashes->new($gal);
69 print "Scanning photos\n";
71 foreach my $src (@source) {
72 my $name = $src->{file};
74 # Try to relativize to OrigDir
75 if (substr($name, 0, length $orig_prefix) eq $orig_prefix) {
76 $src->{file} = $name = substr($name, length $orig_prefix);
81 my $path = File::Spec->rel2abs($name, $gal->get('OrigDir'));
82 -f $path or die "Cannot find $path\n";
84 $src->{id} = $hashes->hash_image($path);
85 print " id=", $src->{id};
87 if (!defined $src->{orientation} || $src->{orientation} eq '-') {
88 my $e = new Image::EXIF($path);
89 my $i = $e->get_all_info();
91 print "EXIF error: ", $e->error, "\n";
92 $src->{orientation} = '.';
94 # print STDERR Dumper($i), "\n";
95 my $o = $i->{'image'}->{'Image Orientation'} || "Top, Left-Hand";
96 if ($o eq "Top, Left-Hand") { $o = "."; }
97 elsif ($o eq "Right-Hand, Top") { $o = "r"; }
98 elsif ($o eq "Left-Hand, Bottom") { $o = "l"; }
99 elsif ($o eq "Bottom, Right-Hand") { $o = "u"; }
101 print "Unrecognized orientation: $o\n";
104 $src->{orientation} = $o;
107 print " ori=", $src->{orientation};
109 if (!defined $src->{xf} || $src->{xf} eq '-') {
110 $src->{xf} = $gal->get('ScanDefaultTransform');
112 print " xfrm=", $src->{xf};
114 $src->{title} //= '';
120 my $old = $gal->read_list('gallery.list');
122 print "Updating gallery.list\n";
123 my %new_by_id = map { $_->{id} => $_ } @images;
127 my $i = $new_by_id{$id};
130 print "\t$id: kept\n";
133 print "\t$id: removed\n";
136 print "\t$id: kept\n";
138 delete $new_by_id{$id};
141 for my $i (@images) {
143 $new_by_id{$id} or next;
144 print "\t$id: new\n";
151 $gal->write_list('gallery.list', \@images);
152 print "Written gallery.list (", (scalar @images), " items)\n";