summaryrefslogtreecommitdiff
path: root/lib/App/Dthumb.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/App/Dthumb.pm')
-rwxr-xr-xlib/App/Dthumb.pm198
1 files changed, 198 insertions, 0 deletions
diff --git a/lib/App/Dthumb.pm b/lib/App/Dthumb.pm
new file mode 100755
index 0000000..2425800
--- /dev/null
+++ b/lib/App/Dthumb.pm
@@ -0,0 +1,198 @@
+package App::Dthumb;
+
+use strict;
+use warnings;
+use autodie;
+use 5.010;
+
+use base 'Exporter';
+
+use App::Dthumb::Data;
+use Archive::Tar;
+use Cwd;
+use Image::Imlib2;
+
+our @EXPORT_OK = ();
+our $VERSION = '0.1';
+
+local $| = 1;
+
+sub new {
+ my ($obj, $conf) = @_;
+ my $ref = {};
+
+ $conf->{size} //= 200;
+ $conf->{spacing} //= 1.1;
+ $conf->{quality} //= 75;
+
+ $ref->{config} = $conf;
+
+ $ref->{data} = App::Dthumb::Data->new();
+ $ref->{tar} = Archive::Tar->new();
+
+ $ref->{html} = $ref->{data}->html_start();
+
+ $ref->{current_file_id} = 0;
+
+ $ref->{config}->{file_index} = 'index.xhtml';
+ $ref->{config}->{file_lightbox} = 'lightbox.js';
+ $ref->{config}->{dir_thumbs} = '.thumbs';
+
+ return bless($ref, $obj);
+}
+
+sub run {
+ my ($self) = @_;
+
+ $self->read_directories();
+ $self->create_files();
+ $self->delete_old_thumbnails();
+ $self->create_thumbnails();
+ $self->write_out_html();
+}
+
+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 | gif | tif) $ }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;
+}
+
+sub create_files {
+ my ($self) = @_;
+ my $thumbdir = $self->{config}->{dir_thumbs};
+ my $fh;
+
+ if (not -d $thumbdir) {
+ mkdir($thumbdir);
+ }
+
+ open($fh, '>', 'lightbox.js');
+ print {$fh} $self->{data}->lightbox();
+ 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}}) {
+ $self->create_thumbnail_html($file);
+ $self->create_thumbnail_image($file);
+ }
+}
+
+sub create_thumbnail_html {
+ my ($self, $file) = @_;
+ my $div_width = $self->{config}->{size} * $self->{config}->{spacing};
+ my $div_height = $div_width + ($self->{config}->{no_names} ? 0 : 10);
+
+ $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\" />\n"
+ . "\t</a>\n",
+ $file,
+ $self->{config}->{dir_thumbs},
+ ($file) x 2,
+ );
+ if (not $self->{config}->{no_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}->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;
+#}