#!/usr/bin/perl ###### # mkgallery - Create a gallery with thumbnails from a directory # Author: Jimmy Scott # Nick: Sick` # Mail: jimmy *at* inet-solutions.be # Address: Jimmy Scott # Goordijk 66 # 2930 BRASSCHAAT # BELGIUM # source: http://pub.devbox.be # $DevBox$ ### # # DEPENDS: ImageMagick # ### # # Copyright (C) 2004 Jimmy Scott # # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # 3. The names of the authors may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF # THE POSSIBILITY OF SUCH DAMAGE. # ###### use strict; use CGI::Carp qw(fatalsToBrowser); use POSIX qw(ceil); print "Content-type: text/html\n\n"; my $convert = "/usr/local/bin/convert"; my $outfile = "index.htm"; my @exts = ("JPG","JPEG"); my $ext; my $hsize = 200; my $vsize = 150; my $cols = 3; my @fotos; my @files; my $foto; my $file; my $thumbdir = "thumbs"; my $error = ""; my $imgcount = 0; # Is ImageMagick installed (convert tool)? if ( ! -f $convert ) { print "File '$convert' not found.\n"; exit; } # Get files in the current directory if ( opendir FH1, '.' ) { @files = grep !/^\.\.?$/, readdir FH1; closedir FH1; } else { print "Cannot open directory '.': Check the directory permissions.\n"; exit; } # Check for write access in the current directory if ( ! open FH2, ">test" ) { print "The webserver has no write permissions in the current directory.\n"; exit; } else { close FH2; unlink "test"; } # Check for thumbdir if ( -d $thumbdir ) { if ( ! open FH3, ">$thumbdir/test" ) { print "The webserver has no write permissions in $thumbdir.\n"; exit; } else { close FH3; unlink "$thumbdir/test"; } } else { print "Directory '$thumbdir' does not exist, "; print "create it and chmod it to allow write access for the webserver, "; print "this is most of the time 777.\n"; exit; } # Filter for fotos with given extentions for $file ( @files ) { for $ext ( @exts ) { if ( $file =~ /\.${ext}$/i ) { if ( -f $file ) { push @fotos, ($file); $imgcount++; } } } } # Create thumbnails for $foto ( @fotos ) { $error .= `$convert -scale ${hsize}x${vsize} "${foto}" "${thumbdir}/th.${foto}"`; } # Create HTML page if ( open FH4, ">$outfile" ) { print FH4 ""; print FH4 ""; my $rows = ceil ( $imgcount / $cols ); for ( ; $rows > 0 ; $rows-- ) { print FH4 ""; for ( 1 .. 3 ) { my $pic = shift @fotos; my $width; if ( $_ == 2 ) { $width = 34; } else { $width = 33; } print FH4 ""; } print FH4 ""; } print FH4 "
"; if ( defined $pic ) { print FH4 ""; print FH4 ""; print FH4 ""; } else { print FH4 " "; } print FH4 "
"; close FH4; } else { print "Could not create $outfile.\n"; exit; } print ""; print "$outfile created.
"; print "$imgcount images recognized and tried to resize.
"; print "errors: $error"; print ""; exit;