1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
package App::Dthumb;
use strict;
use warnings;
use autodie;
use 5.010;
use base 'Exporter';
use App::Dthumb::Data;
use Cwd;
use Image::Imlib2;
use IO::Handle;
use Time::Progress;
our @EXPORT_OK = ();
our $VERSION = '0.1';
STDERR->autoflush(1);
sub new {
my ($obj, $conf) = @_;
my $ref = {};
$conf->{size} //= 200;
$conf->{spacing} //= 1.1;
$conf->{quality} //= 75;
$conf->{lightbox} = !$conf->{'no-lightbox'};
$conf->{names} = !$conf->{'no-names'};
$ref->{config} = $conf;
$ref->{data} = App::Dthumb::Data->new();
$ref->{timer} = Time::Progress->new();
$ref->{html} = $ref->{data}->get('html_start');
$ref->{current_file_id} = 0;
$ref->{config}->{file_index} = 'index.xhtml';
$ref->{config}->{file_lightbox} = 'lightbox.js';
$ref->{config}->{dir_thumbs} = '.thumbs';
$ref->{config}->{dir_data} = '.dthumb';
return bless($ref, $obj);
}
sub run {
my ($self) = @_;
$self->check_cmd_flags();
$self->read_directories();
$self->create_files();
$self->delete_old_thumbnails();
$self->create_thumbnails();
$self->write_out_html();
}
sub check_cmd_flags {
my ($self) = @_;
if ($self->{config}->{version}) {
say "dthumb version ${VERSION}";
exit 0;
}
if ($self->{config}->{help}) {
say "Please refer to perldoc -F $0 (or man dthumb)";
exit 0;
}
}
sub read_directories {
my ($self) = @_;
my $thumbdir = $self->{config}->{dir_thumbs};
my $imgdir = '.';
my $dh;
my (@files, @old_thumbs);
opendir($dh, $imgdir);
for my $file (readdir($dh)) {
if (-f $file and $file =~ qr{ \. (png | jp e? g) $ }iox) {
push(@files, $file);
}
}
closedir($dh);
if (-d $thumbdir) {
opendir($dh, $thumbdir);
for my $file (readdir($dh)) {
if ($file =~ qr{^ [^.] }ox and not -f $file) {
push(@old_thumbs, $file);
}
}
closedir($dh);
}
@{$self->{files}} = sort { lc($a) cmp lc($b) } @files;
@{$self->{old_thumbnails}} = @old_thumbs;
$self->{timer}->attr(
min => 1,
max => scalar @files,
);
}
sub create_files {
my ($self) = @_;
my $thumbdir = $self->{config}->{dir_thumbs};
my $datadir = $self->{config}->{dir_data};
if (not -d $thumbdir) {
mkdir($thumbdir);
}
if ($self->{config}->{lightbox}) {
if (not -d $datadir) {
mkdir($datadir);
}
for my $file (qw(lightbox.js overlay.png loading.gif close.gif)) {
open(my $fh, '>', "${datadir}/${file}");
print {$fh} $self->{data}->get($file);
close($fh);
}
}
}
sub delete_old_thumbnails {
my ($self) = @_;
my $thumbdir = $self->{config}->{dir_thumbs};
for my $file (@{$self->{old_thumbnails}}) {
unlink("${thumbdir}/${file}");
}
}
sub create_thumbnails {
my ($self) = @_;
for my $file (@{$self->{files}}) {
print STDERR $self->{timer}->report(
"\r\e[KCreating Thumbnails: %p done, %L elapsed, %E remaining",
++$self->{current_file_id},
);
$self->create_thumbnail_html($file);
$self->create_thumbnail_image($file);
}
print "\n";
}
sub create_thumbnail_html {
my ($self, $file) = @_;
my $div_width = $self->{config}->{size} * $self->{config}->{spacing};
my $div_height = $div_width + ($self->{config}->{names} ? 10 : 0);
$self->{html} .= sprintf(
"<div style=\"%s; %s; %s; width: %dpx; height: %dpx\">\n",
'text-align: center',
'font-size: 80%',
'float: left',
$div_width,
$div_height,
);
$self->{html} .= sprintf(
"\t<a rel=\"lightbox\" href=\"%s\">\n"
. "\t\t<img src=\"%s/%s\" alt=\"%s\" /></a>\n",
$file,
$self->{config}->{dir_thumbs},
($file) x 2,
);
if ($self->{config}->{names}) {
$self->{html} .= sprintf(
"\t<br />\n"
. "\t<a style=\"%s;\" href=\"%s\">%s</a>\n",
'text-decoration: none',
($file) x 2,
);
}
$self->{html} .= "</div>\n";
}
sub create_thumbnail_image {
my ($self, $file) = @_;
my $thumbdir = $self->{config}->{dir_thumbs};
my $thumb_dim = $self->{config}->{size};
if (-e "${thumbdir}/${file}") {
return;
}
my $image = Image::Imlib2->load($file);
my ($dx, $dy) = ($image->width(), $image->height());
my $thumb = $image;
if ($dx > $thumb_dim or $dy > $thumb_dim) {
if ($dx > $dy) {
$thumb = $image->create_scaled_image($thumb_dim, 0);
}
else {
$thumb = $image->create_scaled_image(0, $thumb_dim);
}
}
$thumb->set_quality($self->{config}->{quality});
$thumb->save("${thumbdir}/${file}");
}
sub write_out_html {
my ($self) = @_;
$self->{html} .= $self->{data}->get('html_end');
open(my $fh, '>', $self->{config}->{file_index});
print {$fh} $self->{html};
close($fh);
}
#sub print_progress {
# my ($self) = @_;
# my $num = $self->{current_file_id};
# my $name = $self->{current_file_name};
#
# if (($num % 60) == 0) {
# if ($number) {
# printf(" %4d/%d\n", $number, scalar(@files));
# }
# printf('[%3d%%] ', $number * 100 / @files);
# } elsif (($number % 10) == 0) {
# print ' ';
# }
# return;
#}
1;
|