#!/usr/bin/perl use Image::Magick; #Gallery by E Hopper http://www.shappyhopper.co.uk #This perl script uses ImageMagick to create a gallery by resizing and annotating images #HTML is output to a file that can be pasted into an existing HTML file or template #The html produced by this script (by default) requires the lightbox.js script which can be downloaded #from http://www.huddletogether.com/projects/lightbox/ #Plus also read the float tutorial on http://css.maxdesign.com.au/floatutorial/tutorial0401.htm #As the default code produced is to create a floating gallery of images that re-sizes to the page width #This script will change images from the local directory to the script. #It will delete the original images unless the lines below (highlighted by lots of hashes) are commented out #Run in a directory with the images to turn into a gallery, and no other files. This script will create directories, and destroy #any with the same name ########################################################################################################################## #This section is where you set the variables for the use of this script #Gallery Images $gallerydirectory="gallery"; #The directory where the gallery images will be kept $bigdimension="640"; #The dimensions of the gallery images $smalldimension="480"; #The script will differenciate between tall and wide images, so set the two dimensions, not the #width and height #Thumbs $thumbnotation="thm_"; #Thumbs name notation $thumbbig="120"; #Thumb dimensions $thumbsmall="90"; $thumbdirectory="thumbs"; #Thumbs direcories #Annotation on main Gallery Image - options for the annotation of your images $AnnotateText="(C) Edwin Hopper - www.shappyhopper.co.uk"; $TextColour="gold"; $TextSize="14"; $TextLocation="SouthWest"; $GeometryX="+40"; $GeometryY="+40"; ########################################################################################################################### #Now we start #Create Directories mkdir $gallerydirectory; mkdir $thumbdirectory; # First We Open the local directory and copy the contents into an array # $DirToGet="."; opendir(IMD, $DirToGet) || die("Cannot Open Directory"); @thefiles=readdir(IMD); closedir(IMD); #Sort array alphabetically @thefiles=sort(@thefiles); #And tell the user what we are doing print "\n\n#############################################################################"; print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!Starting Gallery!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n"; print " This may take some time depending on the size\n"; print " and number of files."; print "\n\n\n This script has found the following files in the target directory:\n"; # # Print the contents of the directory for later troubleshooting # $totalcount=0; foreach $f(@thefiles) { unless ( ($f eq ".") || ($f eq "..") || ($f =~/.pl/) || ($f=~/.cgi/) || ($f =~/.mov/) || ($f =~/.MOV/) || ($f=~/$gallerydirectory/) || ($f=~/$thumbdirectory/) || ($f=~/.DB/) || ($f=~/.db/)) { $totalcount++; print " $totalcount. $f\n"; } } #And start the resizing print "\n\n Begining gallery generation for $totalcount files\n"; #Combine for annotation $GeometryCombineAnn="$GeometryX$GeometryY"; #Make Gallery text File open GALLERYFILE, ">./gallery-code.txt"; print GALLERYFILE "
"; close GALLERYFILE; foreach $f(@thefiles) { unless ( ($f eq ".") || ($f eq "..") || ($f =~/.pl/) || ($f=~/.cgi/) || ($f =~/.html/) || ($f=~/$gallerydirectory/) || ($f=~/$thumbdirectory/) || ($f=~/.DB/) || ($f=~/.db/)) { $counteach++; #This section removes empty spaces from the image name ($nonwhitespacename = $f) =~ s/\s+//g; #This section opens the image print " Reading in $f (file $counteach of $totalcount)\n"; my $thumb= new Image::Magick; open(IMAGE, "$f"); $thumb->Read(file=>\*IMAGE); close(IMAGE); #This gets its dimensions and calculates which way up to resize it my ($height,$width) = $thumb->Get('width','height'); print " Image width $width, Image height $height.\n"; if ($width > $height) { $newwidth=$bigdimension; $newheight=$smalldimension; $thumbwidth=$thumbbig; $thumbheight=$thumbsmall; } else { $newheight=$bigdimension; $newwidth=$smalldimension; $thumbwidth=$thumbsmall; $thumbheight=$thumbbig; } #We have to combine the dimensions to give them to Image Magik #First we combine for the gallery thumb $GeometryCombine="$newwidthX$newheight"; #This section resizes for the gallery image print " Resizing gallery image\n"; $thumb->Resize(geometry=>$GeometryCombine); #And annotate the image print " Annotating image\n"; $thumb->Annotate(text=>$AnnotateText,geometry=>$GeometryCombineAnn,font=>'Generic.ttf', fill=>$TextColour,gravity=>$TextLocation,pointsize=>$TextSize); #Write it to the gallery directory print " Writing Gallery Image\n"; $thumb->Write("./$gallerydirectory/$f"); #Get a fresh copy of the image to make the thumb print " Reading in fresh copy of $f for thumbnail\n"; my $thumb= new Image::Magick; open(IMAGE, "$f"); $thumb->Read(file=>\*IMAGE); close(IMAGE); #And now resize for the thumb image $GeometryCombine="$thumbwidthX$thumbheight"; print " Resizing thumb\n"; $thumb->Resize(geometry=>$GeometryCombine); #Write it to the thumb directory print " Writing Thumb\n"; $thumb->Write("./$thumbdirectory/$thumbnotation$f"); ##################################################################################################################### ############## Comment out this section if you do not want to delete the original file ############################## ##################################################################################################################### if (unlink($f) == 0) { print ""; } else { print ""; } ################################################################################################################### #and add the code to the gallery text file open GALLERYAPPEND, ">>./gallery-code.txt"; #Change this section to the html you want this script to produce. print GALLERYAPPEND <
PAGESECTION close GALLERYAPPEND; print " Done.\n\n\n"; } } #And now finish off the gallery open GALLERYAPPEND, ">>./gallery-code.txt"; #This line of HTML causes the rows of floated images to reset print GALLERYAPPEND < ENDGALLERY close GALLERYAPPEND; print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!Gallery complete!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"; print "#############################################################################\n\n";