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( "