2 # (c) 2003--2012 Martin Mares <mj@ucw.cz>
18 OrigDir => '.', # Original images
19 PhotoDir => 'photo', # Scaled-down photos for web
20 CacheDir => 'cache', # Cache with meta-data and thumbnails
23 PhotoUrlPrefix => 'photo/',
24 ThumbUrlPrefix => 'thumb/',
25 ThemeUrlPrefix => 'gal/',
27 # Processing machinery settings
28 ScanDefaultTransform => 's',
29 PhotoMaxWidth => 1024,
30 PhotoMaxHeight => 1024,
31 ThumbFormats => [ "114x94" ], # Built-in themes use the first size,
32 # but more can be generated
34 # Titles and navigation
35 Title => 'An Unnamed Gallery',
41 return bless $self, $class;
45 my $cfg = "./gallery.cf";
47 unless (defined $self) {
49 die "Error parsing $cfg: $@";
51 die "Cannot load $cfg: $!\n";
53 die "Cannot load $cfg, check that it returns true\n";
59 ### Object methods ###
62 my ($self, $key) = @_;
63 if (exists $self->{cfg}->{$key}) {
64 my $val = $self->{cfg}->{$key};
65 defined $val or warn "Gallery: Config item $key is not set\n";
68 warn "Gallery: Config item $key does not exist\n";
74 my ($self, $key) = @_;
75 return $self->{cfg}->{$key};
80 while (my $key = shift @_) {
82 $self->{cfg}->{$key} //= $val;
88 while (my $key = shift @_) {
89 $self->{cfg}->{$key} = shift @_;
93 sub get_config_keys($) {
95 return keys %{$self->{cfg}};
99 my ($self, $file, $images) = @_;
100 open my $fh, '>:utf8', "$file.new" or die "Cannot create $file.new: $!\n";
101 for my $i (@$images) {
107 ($i->{title} eq '' ? '-' : $i->{title}),
111 rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
115 my ($self, $file) = @_;
117 open my $fh, '<:utf8', $file or return;
123 ($i->{file}, $i->{id}, $i->{orientation}, $i->{xfrm}, $i->{title}) = split /\t/;
124 if ($i->{title} eq '-') { $i->{title} = ""; }
132 my ($self, $file, $meta) = @_;
133 open my $fh, '>', "$file.new" or die "Cannot create $file.new: $!\n";
134 Storable::nstore_fd($meta, $fh);
136 rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
140 my ($self, $file) = @_;
141 open my $fh, '<', $file or die "Cannot read $file: $!\n";
142 my $meta = Storable::fd_retrieve($fh) or die "Cannot parse $file\n";
147 sub photo_meta_name($) {
149 return File::Spec->catfile($self->get('PhotoDir'), 'gallery.meta');
152 sub cache_meta_name($) {
154 return File::Spec->catfile($self->get('CacheDir'), 'cache.meta');
157 sub thumb_fmt_to_size($$) {
158 my ($self, $fmt) = @_;
159 my ($tw, $th) = ($fmt =~ m{^(\d+)x(\d+)$}) or die "Cannot parse thumbnail format $fmt\n";
163 sub photo_file_name($$$) {
164 my ($self, $photo_meta, $id) = @_;
165 return $id . '.' . ($photo_meta->{fmt} // 'jpg');
168 sub photo_name($$$) {
169 my ($self, $photo_meta, $id) = @_;
170 return File::Spec->catfile($self->get('PhotoDir'), $self->photo_file_name($photo_meta, $id));