#!/usr/bin/perl use warnings; use strict; use File::Basename; use Cwd qw(cwd abs_path); use File::MimeInfo qw(mimetype); use Image::Magick; use Getopt::Long; use Module::Load; use Pod::Usage; my $templatedir = abs_path(dirname($0))."/generate_gallery.d"; my $startdir = cwd(); my %opts = (); $opts{template} = "$templatedir/template.html"; $opts{thumbnailsize} = 200; $opts{basepath} = ""; GetOptions(\%opts, "exif|e", "verbose|v", "help|h", "filenamedesc", "template=s", "thumbnailsize|s=i", "htmlonly", "basepath=s") or pod2usage(2); pod2usage(0) if $opts{help}; pod2usage(-verbose => 0) if (@ARGV== 0); if ($opts{exif}) { load Image::ExifTool, "ImageInfo"; }; $opts{template} = abs_path($opts{template}); unless (-r $opts{template}) { print STDERR "Error: template file not readable: $!\n"; exit 1; } for my $dir (@ARGV) { chdir $startdir; unless (-d $dir) { print "Argument is not a directory: $dir. Ignoring.\n" if $opts{verbose}; next; } my $abs_dir = abs_path $dir; chdir $abs_dir; mkdir "thumbs" unless -d "thumbs"; my $html; for my $file (glob("*")) { my $description = ""; unless (mimetype($file) =~ /^image\/.*/) { print "Ignoring non-image: $dir/$file\n" if $opts{verbose}; next; } unless ( -e "$abs_dir/thumbs/$file") { print "Processing $dir/$file ...\n" if $opts{verbose}; my $img = new Image::Magick; $img->Read("$abs_dir/$file"); $img->Thumbnail(geometry=>$opts{thumbnailsize}."x".$opts{thumbnailsize}); $img->Write("$abs_dir/thumbs/$file"); } if ($opts{filenamedesc}) { my $basename = basename($file); $basename =~ s/(.*)\..*/$1/; $description = $basename; $description .= ": " if ($opts{exif}); } if ($opts{exif}) { my $tags = ImageInfo($file, "Aperture", "ShutterSpeed", "FocalLength"); if ($tags->{ShutterSpeed} && $tags->{FocalLength} && $tags->{Aperture}) { $description .= $tags->{ShutterSpeed}."s, ".$tags->{FocalLength}." @ F ".$tags->{Aperture}; } else { print STDERR "Missing at least one needed exif tag for \"$file\". Ignoring.\n"; } } $html .= "
\n"; $html .= "\n"; $html .= "\"\"\n"; $html .= "\n
\n"; } open TEMPLATE, "<", "$opts{template}"; open OUTPUT, ">", "$abs_dir/index.html" or die "Failed to open output file: $!"; while (