<?xml version="1.0" encoding="windows-1252"?>
<node id="66034" title="SHTML generator for image viewing" created="2001-03-21 14:13:15" updated="2005-08-11 05:42:13">
<type id="1748">
sourcecode</type>
<author id="16711">
djw</author>
<data>
<field name="doctext">
&lt;code&gt;
#!/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_question );

#
# 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 page 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 sensitive 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.  yes 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 name
$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 reading: ($!)\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 = &lt;STDIN&gt; );
		if ( $creation_answer eq "yes" ) 
		{ 
	    	mkdir( "$webpage_dir", 0775 ) || die "Can't create $webpage_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 = &lt;STDIN&gt; );
		if ( $creation_answer2 eq "yes" ) 
		{
			mkdir( "$webpage_dir/$pic_dir", 0775 ) || die "Can't create $webpage_dir/$pic_dir: ($!)\n";
			if ( $file_copy eq "yes" ) 
			{
				print "$webpage_dir/$pic_dir is there, now to copy files.\n";
				print "Copying files.....\n";
				copy_files();
			}
		} 
		else 
		{
			print "Doesn't look like you have $webpage_dir/$pic_dir and 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 &lt;= $pics_page ) 
	{
		write_single_shtml();
	} 
	elsif ( $num_pics &gt; $pics_page ) 
	{ 
		write_multi_shtml(); 
	}
}

sub write_single_shtml
{
	open( SHTML, "+&gt;$webpage_dir/$web_file$page_num.shtml" ) || die "Can't open file for writing: ($!)\n";
	print SHTML "&lt;!--#include virtual=\"$header\"--&gt;\n";
	
	print SHTML "&lt;table border=\"0\" width=\"100%\"&gt;&lt;tr&gt;&lt;td align=\"left\" width=\"50%\" valign=\"top\"&gt;";
	print SHTML "Automatic Pic Viewer by:&lt;b&gt; David Wakeman, 2001&lt;/b&gt;&lt;/td&gt;\n";
	if ($logo_question eq "yes")
	{
		print SHTML "&lt;td align=\"right\" width=\"%50\"&gt;&lt;img src=\"$logo\" border=\"0\" align=\"right\"&gt;&lt;/td&gt;\n";
	}
	print SHTML "&lt;/tr&gt;&lt;/table&gt;\n";
														
	
	print SHTML "&lt;center&gt;\n";
	foreach $pic ( @pictures )
	{
		print SHTML "&lt;img src=\"$pic_dir/$pic\" border=\"0\"&gt;&lt;br&gt;\n";
		if ( $comments eq "yes" )
		{
			print "What is the comment for $pic? ";
			chomp( $comment = &lt;STDIN&gt; );
			print SHTML "[ $comment ]\n";
			print SHTML "&lt;br&gt;&lt;br&gt;\n";
		}
		else
		{
			print SHTML "[ &lt;font size=\"1\" face=\"arial\"&gt;$pic&lt;/font&gt; ]\n";
			print SHTML "&lt;br&gt;&lt;br&gt;\n";
		}
	}
	print SHTML "&lt;/center&gt;\n";
	print SHTML "&lt;!--#include virtual=\"$footer\"--&gt;\n";
	close( SHTML );
	print "Done.\n";
}


sub write_multi_shtml 
{
	open( MENU, "+&gt;$webpage_dir/$menu" ) || die "Can't open menu for writing: ($!)\n";
	print MENU "&lt;center&gt;\n";
	print MENU "&lt;hr width=\"400\"&gt;\n";
	while (@pictures) 
	{
		@new_pictures = splice(@pictures, 0, $pics_page);
		open( SHTML, "+&gt;$webpage_dir/$web_file$page_num.shtml" ) || die "Can't open file for writing: ($!)\n";
		print SHTML "&lt;!--#include virtual=\"$header\"--&gt;\n";
		print SHTML "&lt;table border=\"0\" width=\"100%\"&gt;&lt;tr&gt;&lt;td align=\"left\" width=\"50%\" valign=\"top\"&gt;";
		print SHTML "Automatic Pic Viewer by:&lt;b&gt; David Wakeman, 2001&lt;/b&gt;&lt;/td&gt;\n";
		if ($logo_question eq "yes")
		{
			print SHTML "&lt;td align=\"right\" width=\"%50\"&gt;&lt;img src=\"$logo\" border=\"0\" align=\"right\"&gt;&lt;/td&gt;\n";
		}
		print SHTML "&lt;/tr&gt;&lt;/table&gt;\n";
		print SHTML "&lt;!--#include virtual=\"$menu\"--&gt;\n";
		print SHTML "&lt;center&gt;\n";
		foreach $pic ( @new_pictures ) 
		{
			print SHTML "&lt;img src=\"$pic_dir/$pic\" border=\"0\" width=\"400\" height=\"268\"&gt;&lt;/a&gt;&lt;br&gt;\n";
            		if ( $comments eq "yes" ) 
			{
				print "What is the comment for $pic? ";
				chomp( $comment = &lt;STDIN&gt; );
				print SHTML "[ $comment ]\n";
				print SHTML "&lt;br&gt;&lt;br&gt;\n";
	    		} 
			else 
			{
				print SHTML "[ &lt;font size=\"1\" face=\"arial\"&gt;$pic&lt;/font&gt; ]\n";
				print SHTML "&lt;br&gt;&lt;br&gt;\n";
	    		}
		}
		print SHTML "&lt;/center&gt;\n";
	   	print MENU " -[ &lt;a href=\"$web_file$page_num.shtml\"&gt;Page $page_num&lt;/a&gt; ]-";
		print SHTML "&lt;!--#include virtual=\"$menu\"--&gt;\n";
		print SHTML "&lt;!--#include virtual=\"$footer\"--&gt;\n";
		$page_num++;
    }
	print MENU "&lt;/center&gt;\n";
	print MENU "&lt;br&gt;&lt;hr width=\"400\"&gt;\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";
}
&lt;/code&gt;

</field>
<field name="codedescription">
&lt;br&gt;
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.&lt;br&gt;&lt;br&gt;
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.&lt;br&gt;&lt;br&gt;
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.
&lt;br&gt;&lt;br&gt;
You can see a demo at &lt;a href="http://www.gibfest.org/pictures/pictures1.shtml"&gt;http://www.gibfest.org/pictures/pictures1.shtml&lt;/a&gt; which has some pics from around my office.&lt;br&gt;&lt;br&gt;
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.&lt;br&gt;&lt;br&gt;
ciao,
[djw]</field>
<field name="codecategory">
Web Stuff</field>
<field name="codeauthor">
[djw]&lt;br&gt;
&lt;a href="mailto:djw@gibfest.org"&gt;djw@gibfest.org&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.gibfest.org/"&gt;http://www.gibfest.org/&lt;/</field>
</data>
</node>
