Welcome to the Monastery. The task you are attempting is not a trivial
one, but, if you are willing to give it a try, you will find that Perl
is a great tool for the job.
You need to look at the job in front of you in terms of input and
output. Your input seems to be the name of a directory. Given the
directory name, you want to write a script that will look into the
directory, get the names of the image files, and the description from
the text file, and produce an html file with the gallery - your desired
output. Since you probably want to produced galleries with consistent
look, you need to look at some templating tools to make your job
easier. I would recommend HTML::Template.
Start with creating a basic template file, gallery.tmpl.
<html>
<head>
<style>
div.img {
margin: 5px; padding: 5px;
border: 1px solid #0000ff;
height: auto; width: auto; float: left;
text-align: center;
}
div.img img {
display: inline; margin: 5px; border: 1px solid #ffffff;
}
div.img a:hover img {
border:1px solid #0000ff;
}
</style>
</head>
<body>
<p>
<TMPL_VAR NAME=DESCR>
</p>
<TMPL_LOOP NAME=GALLERY>
<div class="img">
<a target="_blank" href="<TMPL_VAR NAME=IMG_BIG>">
<img src="<TMPL_VAR NAME=IMG_SMALL>" width="110" height="90">
</a>
</div>
</TMPL_LOOP>
</body>
</html>
It contains one description field, and a loop
that will create appropriate div blocks for every image.
Try it out with the following code.
#!/usr/bin/perl
use v5.14;
use HTML::Template;
my $template = HTML::Template->new(filename => 'gallery.tmpl');
my @gallery;
for my $count (1..3) {
my %data = (
img_small => "img$count-small.jpg",
img_big => "img$count-big.jpg",
);
push @gallery, \%data;
}
$template->param(DESCR => "Some general description");
$template->param(GALLERY => \@gallery);
print $template->output;
If the names of the images in your directory happen to be 1-small.jpg,
1-big.jpg, and so on, and you are satisfied with the predefined string
"Some general description" as your description, this could be what you
want. But it probably isn't. Right now, we're producing the
output, but we're ignoring the input completely.
The next step would be to read the description from a file. Take a
look at the File::Slurp module, and use it to read a plain
text file in a given directory. Then, substitute the DESCR param with
the description gathered from your file (the file contents). Then,
take a look at Find all JPEG files in a directory, and expand the script a bit more. Then,
your job will hopefully be finished.
Good luck.
|