Skip to content

Instantly share code, notes, and snippets.

@hikiko
Last active October 11, 2022 01:35
Show Gist options
  • Save hikiko/9957ae62f4f8908c79249f7c1715dbb9 to your computer and use it in GitHub Desktop.
Save hikiko/9957ae62f4f8908c79249f7c1715dbb9 to your computer and use it in GitHub Desktop.
perl script to create an index.html from a dir of images
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Getopt::Long;
use File::MimeInfo;
my @files;
my $help;
my $filetype;
my $dir;
GetOptions("d=s" => \$dir,
"h" => \$help);
if ($help) {
print qq[Help:
-d <directory>, png images directory
-h, print this help and exit]."\n";
exit;
}
if (!$dir) {
$dir = ".";
}
die "Directory $dir not found.".$!."\n" unless -d $dir;
opendir(DIR, $dir) or die "Failed to open $dir: ".$!."\n";
@files = (readdir DIR) or die $!."\n";
closedir(DIR) or die $!."\n";
my $index = "$dir/index.html";
open FP, ">", $index;
print FP qq[
<html>
<body>
<table>
<tr><th>Image</th><th>Filename</th></tr>];
my $mime_info = File::MimeInfo->new();
foreach (@files) {
next if (!$_ || $_ eq "." || $_ eq ".." || -d "$dir/$_" );
$filetype = $mime_info->globs($_);
next unless $filetype eq qq[image/png];
print FP qq[
<tr><td><img src="$_" alt="$_"></td>];
$_ =~ s{\.[^.]+$}{};
print FP qq[<td>$_</td></tr>];
}
print FP qq[
</table>
</body>
</html>];
close FP or die $!."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment