http://www.perlmonks.org?node_id=66034
Category: Web Stuff
Author/Contact Info djw
djw@gibfest.org
http://www.gibfest.org/</
Description:
This utility takes a directory of images, copies files to a specified dir, creates shtml page for each block of pictures (how many per block is decided in config), can prompt for a description for each pic (otherwise uses image name), and creates a menu.shtml and an shtml page for each block needed depending again on how many pics per page you want and how many pics you have.

It requires you to have server-side includes enabled in your specified web dir so that you can include header, footer, and menu server-side include files. It also puts an image at the top right of each page, and a "created by" thing at the top left - but you can change that to have some other text, or nothing if you like.

I thought about expanding this thing to use a text file for image descriptions, and adding html support, but I don't need those features so I decided not to. It certainly can be done easily enough.

You can see a demo at http://www.gibfest.org/pictures/pictures1.shtml which has some pics from around my office.

btw, I'm really sorry about the spacing that got mangled by using notepad. This was written in Vi with a 4 space tab, but got converted because I posted this from a win32 box and I used notepad..../me sighs.

ciao, djw
#!/usr/bin/perl -w
#
# David Wakeman, 2001
# http://www.gibfest.org/
# djw@gibfest.org
#
# Utility to sort a bunch of image's into formatted html pages
# so they can be viewed through a web browser.  Also includes
# the ability to write comments for each picture - requires
# user input.
#
# shtml assumes apache with Inlcudes module loaded.  I have no
# clue how other web servers deal with this type of thing.

use strict;

my( $web_file, $webpage_dir, $comment, $comments, $file, $logo );
my( @pictures, $num_pics, $pic, $pics_page, $image_type, $image_dir);
my( $file_copy, $creation_answer, $page_num, $menu, $footer);
my( $pic_dir, $creation_answer2, @new_pictures, $header, $logo_questio
+n );

#
# Configuration area.  Please enter everything you need or require
# here.  You shouldn't need to edit past this point.  Please keep 
# all answers lowercase.  I'm too lazy to check for that right now    
+                                            

#
# These are all the directories we need to define.  $image_dir 
# is where you want to grab the images from. $webpage_dir is
# where you will put your shtml files.  And $pics_dir is where
# the images will be copied to.

$image_dir = "/home/djw/Perl/work/images";
$webpage_dir = "/var/www/pictures";
$pics_dir = "images";

#
# File name to use for each page.  This will be incremented for each p
+age you
# have depending on how many pics you have, and how many pics you want
+ displayed
# per page. Also, define the image type you want to use (case sensitiv
+e for now)
$web_file = "pictures";
$image_type = "jpg";

#
# How many pics do you want displayed per page.
$pics_page = 10;

#
# Do you want this program to copy your images over to the webidr? yes
+ or no only.
$file_copy = "yes";

#
# Answer here if you would like to include comments under each pic.  y
+es or no only.
$comments = "no";

#
# Name of your server-side includes files
$header = "header.shtml";
$menu = "menu.shtml";
$footer = "footer.shtml";


#
# Answer here if you would like a logo, if yes, then enter the logo na
+me
$logo_question = "yes";
$logo = "pictures.jpg";

#
# No touchy after this point unless you know what you are 
# doing.  If you do know, then please let ME know what cool
# things you have done.

$pic = "";
$creation_answer = "";
$creation_answer2 = "";
$comment = "";
$page_num = 1;

dir_glob();
create_dirs();
write_web();

sub dir_glob 
{
    opendir( IDIR, "$image_dir" ) || die "Can't open $image_dir for re
+ading: ($!)\n";
    foreach $file ( readdir( IDIR ) ) 
    {
        if ($file =~ /(.*?)\.$image_type/ ) { push(@pictures, $file); 
+}
    }
    closedir( IDIR );
    $num_pics = @pictures;
}        


sub create_dirs 
{
    if ( -d "$webpage_dir/$pic_dir" ) 
    {
        if ( $file_copy eq "yes" ) 
        {
            print "Copying files....\n";
            copy_files(); 
        } 
    }
    else 
    {
        print "Directory $webpage_dir doesn't exist.\n";
        print "Want to create the \$web_dir: $webpage_dir (yes or no)?
+ ";
        chomp ( $creation_answer = <STDIN> );
        if ( $creation_answer eq "yes" ) 
        { 
            mkdir( "$webpage_dir", 0775 ) || die "Can't create $webpag
+e_dir: ($!)\n";
        } 
        else 
        { 
            print "Skipping $webpage_dir creation.\n"; 
        }
        print "Directory $webpage_dir/$pic_dir doesn't exist.\n";
        print "Want to create it (yes or no)? ";
        chomp( $creation_answer2 = <STDIN> );
        if ( $creation_answer2 eq "yes" ) 
        {
            mkdir( "$webpage_dir/$pic_dir", 0775 ) || die "Can't creat
+e $webpage_dir/$pic_dir: ($!)\n";
            if ( $file_copy eq "yes" ) 
            {
                print "$webpage_dir/$pic_dir is there, now to copy fil
+es.\n";
                print "Copying files.....\n";
                copy_files();
            }
        } 
        else 
        {
            print "Doesn't look like you have $webpage_dir/$pic_dir an
+d I won't create it, exiting\n";
            exit;
        }
    }
}


sub copy_files 
{
    system( "cp $image_dir/*.$image_type $webpage_dir/$pic_dir" );
    print "Done.\n";
}
                        

sub write_web 
{
    if ( $num_pics <= $pics_page ) 
    {
        write_single_shtml();
    } 
    elsif ( $num_pics > $pics_page ) 
    { 
        write_multi_shtml(); 
    }
}

sub write_single_shtml
{
    open( SHTML, "+>$webpage_dir/$web_file$page_num.shtml" ) || die "C
+an't open file for writing: ($!)\n";
    print SHTML "<!--#include virtual=\"$header\"-->\n";
    
    print SHTML "<table border=\"0\" width=\"100%\"><tr><td align=\"le
+ft\" width=\"50%\" valign=\"top\">";
    print SHTML "Automatic Pic Viewer by:<b> David Wakeman, 2001</b></
+td>\n";
    if ($logo_question eq "yes")
    {
        print SHTML "<td align=\"right\" width=\"%50\"><img src=\"$log
+o\" border=\"0\" align=\"right\"></td>\n";
    }
    print SHTML "</tr></table>\n";
                                                        
    
    print SHTML "<center>\n";
    foreach $pic ( @pictures )
    {
        print SHTML "<img src=\"$pic_dir/$pic\" border=\"0\"><br>\n";
        if ( $comments eq "yes" )
        {
            print "What is the comment for $pic? ";
            chomp( $comment = <STDIN> );
            print SHTML "[ $comment ]\n";
            print SHTML "<br><br>\n";
        }
        else
        {
            print SHTML "[ <font size=\"1\" face=\"arial\">$pic</font>
+ ]\n";
            print SHTML "<br><br>\n";
        }
    }
    print SHTML "</center>\n";
    print SHTML "<!--#include virtual=\"$footer\"-->\n";
    close( SHTML );
    print "Done.\n";
}


sub write_multi_shtml 
{
    open( MENU, "+>$webpage_dir/$menu" ) || die "Can't open menu for w
+riting: ($!)\n";
    print MENU "<center>\n";
    print MENU "<hr width=\"400\">\n";
    while (@pictures) 
    {
        @new_pictures = splice(@pictures, 0, $pics_page);
        open( SHTML, "+>$webpage_dir/$web_file$page_num.shtml" ) || di
+e "Can't open file for writing: ($!)\n";
        print SHTML "<!--#include virtual=\"$header\"-->\n";
        print SHTML "<table border=\"0\" width=\"100%\"><tr><td align=
+\"left\" width=\"50%\" valign=\"top\">";
        print SHTML "Automatic Pic Viewer by:<b> David Wakeman, 2001</
+b></td>\n";
        if ($logo_question eq "yes")
        {
            print SHTML "<td align=\"right\" width=\"%50\"><img src=\"
+$logo\" border=\"0\" align=\"right\"></td>\n";
        }
        print SHTML "</tr></table>\n";
        print SHTML "<!--#include virtual=\"$menu\"-->\n";
        print SHTML "<center>\n";
        foreach $pic ( @new_pictures ) 
        {
            print SHTML "<img src=\"$pic_dir/$pic\" border=\"0\" width
+=\"400\" height=\"268\"></a><br>\n";
                    if ( $comments eq "yes" ) 
            {
                print "What is the comment for $pic? ";
                chomp( $comment = <STDIN> );
                print SHTML "[ $comment ]\n";
                print SHTML "<br><br>\n";
                } 
            else 
            {
                print SHTML "[ <font size=\"1\" face=\"arial\">$pic</f
+ont> ]\n";
                print SHTML "<br><br>\n";
                }
        }
        print SHTML "</center>\n";
           print MENU " -[ <a href=\"$web_file$page_num.shtml\">Page $
+page_num</a> ]-";
        print SHTML "<!--#include virtual=\"$menu\"-->\n";
        print SHTML "<!--#include virtual=\"$footer\"-->\n";
        $page_num++;
    }
    print MENU "</center>\n";
    print MENU "<br><hr width=\"400\">\n";
    close( SHTML );
        close( MENU );
    print "Copied $num_pics images to $webpage_dir/$pic_dir/\n";
    print "Done writing $page_num shtml to $webpage_dir.\n";
}
Replies are listed 'Best First'.
Re: SHTML generator for image viewing
by Hero Zzyzzx (Curate) on Mar 23, 2001 at 00:04 UTC

    Output looks great! I've done some similar work to this, available here. Hope you like baby pictures! This was one of my first perl/mySQL projects, and as a consquence the code is a mess! It has the ability to use multiple templates, picture comments, and a few other features.

    In you code, what about using image magick to create thumbnails, then having the thumbnails link to a template containing the full size picture?

      Hey that is awesome! You have an uber-image viewer thing...

      Never thought of using a template like that. I kind of thought that I could change everything using shtml but now you have me thinking. I had thought about providing thumbnails using Image::Magick but I needed to get the code finished and didn't have time to look at the module - but its something I definately want to add asap.

      Now that I see yours though...hahaha that is a fine menu and I like the "next" and "back" buttons.

      Thanks,
      djw

        Well, your system has advantages because of it's use of SSI, it's probably quite a bit faster than my "each page created on the fly" system. I'll make sure to watch what you come up with.

        My image viewer is an abandonded business idea I had. I might revisit it just for my own fun. Randall Schwartz made a really neat image viewer here.