2 # UCW Gallery: Scan images and generate image list
3 # (c) 2004--2015 Martin Mares <mj@ucw.cz>
8 use UCW::Gallery::Hashes;
13 if (@ARGV && $ARGV[0] eq '--help') {
15 Usage: cat list | gal scan <options>
16 or: gal scan <options> <files and directories>
17 or: gal scan <options> --update
20 --add Keep existing images and add new ones
28 'update!' => \$update,
29 ) or die "Try gal scan --help\n";
32 my $gal = UCW::Gallery->load_config;
33 my $orig_prefix = $gal->get('OrigDir');
34 $orig_prefix =~ m{/$} or $orig_prefix .= '/';
38 print "Loading previous gallery.list\n";
39 my $pg = $gal->read_list('gallery.list') or die "Unable to load gallery.list\n";
44 push @source, { file => $in };
46 opendir D, $in or die "Cannot scan directory $in: $!\n";
48 while (my $e = readdir D) {
49 my $f = File::Spec->canonpath(File::Spec->catfile($in, $e));
50 if ($f =~ m{\.(jpe?g|png)$}i) {
55 push @source, map { { file => $_ } } sort @p;
57 die "$in is neither file nor directory\n";
61 binmode STDIN, ':utf8';
62 @source = @{$gal->read_list_fh(\*STDIN)};
65 my $hashes = UCW::Gallery::Hashes->new($gal);
67 print "Scanning photos\n";
69 foreach my $src (@source) {
70 my $name = $src->{file};
72 # Try to relativize to OrigDir
73 if (substr($name, 0, length $orig_prefix) eq $orig_prefix) {
74 $src->{file} = $name = substr($name, length $orig_prefix);
79 my $path = File::Spec->rel2abs($name, $gal->get('OrigDir'));
80 -f $path or die "Cannot find $path\n";
82 $src->{id} = $hashes->hash_image($path);
83 print " id=", $src->{id};
85 if (!defined $src->{orientation} || $src->{orientation} eq '-') {
86 my $e = new Image::EXIF($path);
87 my $i = $e->get_all_info();
89 print "EXIF error: ", $e->error, "\n";
90 $src->{orientation} = '.';
92 # print STDERR Dumper($i), "\n";
93 my $o = $i->{'image'}->{'Image Orientation'} || "Top, Left-Hand";
94 if ($o eq "Top, Left-Hand") { $o = "."; }
95 elsif ($o eq "Right-Hand, Top") { $o = "r"; }
96 elsif ($o eq "Left-Hand, Bottom") { $o = "l"; }
97 elsif ($o eq "Bottom, Right-Hand") { $o = "u"; }
99 print "Unrecognized orientation: $o\n";
102 $src->{orientation} = $o;
105 print " ori=", $src->{orientation};
107 defined $src->{xf} or $src->{xf} = $gal->get('ScanDefaultTransform');
108 print " xfrm=", $src->{xf};
110 $src->{title} //= '';
116 my $old = $gal->read_list('gallery.list');
118 print "Updating gallery.list\n";
119 my %new_by_id = map { $_->{id} => $_ } @images;
123 my $i = $new_by_id{$id};
126 print "\t$id: kept\n";
129 print "\t$id: removed\n";
132 print "\t$id: kept\n";
134 delete $new_by_id{$id};
137 for my $i (@images) {
139 $new_by_id{$id} or next;
140 print "\t$id: new\n";
147 $gal->write_list('gallery.list', \@images);
148 print "Written gallery.list (", (scalar @images), " items)\n";