Skip to content

Instantly share code, notes, and snippets.

@alexpreynolds
Last active August 29, 2015 14:18
Show Gist options
  • Save alexpreynolds/a7313ba089c480edad03 to your computer and use it in GitHub Desktop.
Save alexpreynolds/a7313ba089c480edad03 to your computer and use it in GitHub Desktop.
Sets up a SimpleViewer-based image gallery
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use File::Basename;
use File::Path;
use File::Copy;
use Getopt::Long;
use IO::File;
use lib "/home/areynolds/opt/perl/lib/perl5/site_perl/5.10.0";
use XML::Writer;
# -------------------------------------------------------------------------------------------
# options
my ($inputDir, $outputDir, $galleryTitle, $rootHTMLURL);
my ($result) = GetOptions("inputDir=s" => \$inputDir,
"outputDir=s" => \$outputDir,
"galleryTitle=s" => \$galleryTitle,
"rootHTMLURL=s" => \$rootHTMLURL,
);
if (!$inputDir) { die "ERROR: Specify --inputDir=dirName\n"; }
if (!$outputDir) { die "ERROR: Specify --outputDir=dirName\n"; }
if (!$galleryTitle) { die "ERROR: Specify --galleryTitle=title\n"; }
if (!$rootHTMLURL) { die "ERROR: Specify --rootHTMLURL=url\n (e.g. http://www.uwencode.org/~areynolds)"; }
# -------------------------------------------------------------------------------------------
# variables
my $convertBin = "/usr/bin/convert";
my $imageFileExtension;
my @supportedFileExtensions = qw(jpg png gif);
my $imageFile;
my $imageFileName;
my @imageFiles = ();
my $rootGalleryDir = $outputDir;
my $rootImagesDir = "$rootGalleryDir/images";
my $rootThumbsDir = "$rootGalleryDir/thumbs";
my $thumbnailWidth = 140;
my $subtitle;
# gallery XML options (cf. http://simpleviewer.net/simpleviewer/pro/support/pro_options.html)
my $maxImageWidth = 3200;
my $maxImageHeight = 1600;
my $textColor = "0xFFFFFF";
my $backgroundColor = "0xFFFFFF";
my $imageBackColor = "0xFFFFFF";
my $imageBackOpacity = "1";
my $frameColor = "0xFFFFFF";
my $frameWidth = "10";
my $stagePadding = "40";
my $navPadding = "40";
my $thumbnailColumns = "3";
my $thumbnailRows = "5";
my $navPosition = "left";
my $vAlign = "center";
my $hAlign = "center";
my $title = "$galleryTitle";
my $enableRightClickOpen = "true";
my $backgroundImagePath = "";
my $imagePath = "";
my $thumbPath = "";
my $imageDropShadow = "FALSE";
my $imageTransitionType = "SLIDE";
my $changeCaptionOnHover = "TRUE";
# simpleviewer
my $galleryXMLOutput;
my $galleryXML;
my $galleryFile = "$rootGalleryDir/gallery.xml";
my $svDir = "/home/areynolds/proj/profiler/sv/simpleviewer-pro/web";
if (! -d $svDir ) { die "ERROR: $svDir Simpleviewer resources are missing\n"; }
# -------------------------------------------------------------------------------------------
# main
# build a list of files to copy over
foreach $imageFileExtension (@supportedFileExtensions) {
foreach $imageFile (<$inputDir/*.$imageFileExtension>) {
push (@imageFiles, $imageFile);
}
}
# build URL
my @urlElems = split("/", $inputDir);
my $rootURL = "$rootHTMLURL/".join("/", @urlElems[4..(scalar @urlElems - 1)]);
# make outputDir/images and outputDir/thumbs subdirectories
if (-d "$rootGalleryDir") { warn "$rootGalleryDir exists\n"; rmtree($rootGalleryDir); mkpath($rootGalleryDir); } else { mkpath ("$rootGalleryDir"); }
if (-d "$rootImagesDir") { warn "$rootImagesDir exists\n"; } else { mkpath ("$rootImagesDir"); }
if (-d "$rootThumbsDir") { warn "$rootThumbsDir exists\n"; } else { mkpath ("$rootThumbsDir"); }
# copy simpleviewer resources to root output dir
system ("cp -R $svDir/index.html $svDir/svcore $rootGalleryDir") == 0 or die "ERROR: Could not copy SimpleViewer resources to $rootGalleryDir: $?\n";
# make XML and convert
$galleryXMLOutput = new IO::File("> $galleryFile");
$galleryXML = new XML::Writer(OUTPUT => $galleryXMLOutput, NEWLINES => 1, DATA_INDENT => 1);
$galleryXML->xmlDecl('UTF-8');
$galleryXML->startTag('simpleviewergallery', maxImageWidth=>$maxImageWidth, maxImageHeight=>$maxImageHeight, textColor=>$textColor, frameColor=>$frameColor, frameWidth=>$frameWidth, stagePadding=>$stagePadding, navPadding=>$navPadding, thumbnailColumns=>$thumbnailColumns, thumbnailRows=>$thumbnailRows, navPosition=>$navPosition, vAlign=>$vAlign, hAlign=>$hAlign, title=>$title, enableRightClickOpen=>$enableRightClickOpen, backgroundImagePath=>$backgroundImagePath, imagePath=>$imagePath, thumbPath=>$thumbPath, backgroundColor=>$backgroundColor, imageBackColor=>$imageBackColor, imageBackOpacity=>$imageBackOpacity, imageDropShadow=>$imageDropShadow, imageTransitionType=>$imageTransitionType, changeCaptionOnHover=>$changeCaptionOnHover);
# copy images to outputDir/images, make thumbnail and copy to outputDir/thumbs, and make XML entry
foreach $imageFile (@imageFiles) {
my @imageFileNameElements = fileparse($imageFile);
$imageFileName = $imageFileNameElements[0];
$subtitle = $imageFileName;
#
# Uncomment the copy() call and comment the if-block to copy images, instead of making symbolic links.
# Copying is slower than making symlinks, but it gives a more self-contained product.
#
#copy($imageFile, $rootImagesDir) or die "ERROR: File $imageFile cannot be copied!\n";
if ("$imageFile" ne "$rootImagesDir/$imageFileName") {
system ("ln -s $imageFile $rootImagesDir/$imageFileName") == 0 or die "could not symlink\n";
}
system ("$convertBin -thumbnail $thumbnailWidth \"$imageFile\" \"$rootThumbsDir/$imageFileName\"") == 0 or die "ERROR: could not make thumbnail from $imageFile to $rootThumbsDir/$imageFileName: $?\n";
# write XML
$galleryXML->startTag('image', imageURL=>"images/$imageFileName", thumbURL=>"thumbs/$imageFileName", linkURL=>"$rootURL/$imageFileName", linkTarget=>"_blank");
$galleryXML->dataElement('caption' => "$subtitle");
$galleryXML->endTag('image');
}
# close XML file
$galleryXML->endTag('simpleviewergallery');
$galleryXML->end();
$galleryXMLOutput->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment