#!/usr/bin/perl -lw # # galeryja # # Copyright (c) 2003 Krzysztof Krzyzaniak # # You may distribute under the terms of either the GNU General Public # License[1] or the Artistic License[2]. # # [1] http://www.gnu.org/licenses/gpl.html # [2] http://www.perl.com/pub/a/language/misc/Artistic.html # # $Id: galeryja 67 2005-07-29 12:05:27Z eloy $ use Image::Magick; use Data::Dumper; use Cwd; use Getopt::Long; use File::Path; use File::Basename; use File::Glob qw(:globally); use vars qw( $version ); use strict; $version = do { '$Id: galeryja 67 2005-07-29 12:05:27Z eloy $' =~ /\s(\d{4}\-\d{2}\-\d{2})\s/; $1;}; my $Config = { minx => 128, miny => 96, maxwidth => 760, maxheight => 570, types => [ 'png', 'gif', 'jpg', 'jpeg' ], bgcolor => '#ffffff', textcolor => '#000000', bordercolor => '#add8e6', linkcolor => '#555555', thumbdir => '_thumbs', pictures => 40, clean => 0, title => '' }; my $have_jhead = 0; my $jhead_cmd = 'jhead'; ############################################################################# ### sub max($$) { my ( $x, $y ) = @_; return $x <= $y ? $y : $x; } ############################################################################# ### get_now - return string with actual time sub get_now() { my ( $sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst ) = localtime(time); return sprintf( "%2d-%2d-%4d %2d:%2d", $day, $mon + 1, $year + 1900, $hour, $min ); } ## # create_miniature($source, $destination) sub create_miniature($$) { my ( $source, $destination ) = @_; my $pic = Image::Magick->new; $pic->Read($source); my $picxsize = $pic->Get('width'); my $picysize = $pic->Get('height'); if ($picxsize && $picysize) { my $minx = $Config->{'minx'} < $picxsize ? $Config->{'minx'} : $picxsize; my $miny = $Config->{'miny'} < $picysize ? $Config->{'miny'} : $picysize; my $montage = $pic->Montage("geometry" => sprintf("%dx%d>", $minx-2, $miny-2)); $montage->Border(fill=>$Config->{bordercolor}, width=>1, height => 1); $montage->Write( "filename" => $destination, compression => 'None'); if ( $picxsize > $Config->{maxwidth} || $picysize > $Config->{maxheight} ) { my $thfile = get_file_thfile( $destination ); $pic->Resize( "geometry" => sprintf("%dx%d>", $Config->{maxwidth}, $Config->{maxheight})); $pic->Write( "filename" => $thfile ); } } else { print qq{Something wrong is with $source. Skipped\n}; } } ############################################################################# ### sub get_info($$) { my ( $image, $what ) = @_; my $pic = Image::Magick->new; $pic->Read($image); return $pic->Get($what); } ############################################################################# ### sub get_files() { opendir( DIR, "." ); my @pliki = readdir DIR; closedir DIR; my @p = (); for my $t ( @{ $Config->{types} } ) { push @p, '\.' . $t . '$'; } my $pattern = join( '|', @p ); @pliki = sort grep { /$pattern/i } @pliki; return \@pliki; } ############################################################################# ### do_help() - wypisz help na stdout sub do_help() { print < Options: --minx x - thumbnail width (default 128 pixels) --miny y - thumbnail height (default 96 pixels) --thumbdir dir - folder for thumbnails and HTML files --bgcolor color - background colour in HTML notation (default #ffffff) --textcolor color - text colour in HTML notation (default #000000) --types ext - types of image files (default png, gif, jpg i jpeg) --overwrite - allow overwriting --f - - // - --help - this help --h - - // - --clean - remove old files (indexes & thumbs directory) first EOF ; } ## # do_miniature($) - make thumbnail & return HTML code # sub do_miniature($) { my $image = shift; my $miniature = sprintf( "%s/%s", $Config->{thumbdir}, $image ); create_miniature( $image, $miniature ); my $fsize = sprintf( "(%.2f KB)", get_info( $image, "filesize" ) / 1024 ); my $link = sprintf( "%s/%s.html", $Config->{thumbdir}, $image ); $link =~ s/\ /%20/g; $miniature =~ s/\ /%20/g; $image = short_filename($image); return qq {
$image
[$image]
${fsize}
}; } ## # get_scale_image($width, $height) # sub get_scale_image($$) { my ($width, $height) = @_; if ($width > $Config->{maxwidth}) { $height = ($Config->{maxwidth}*$height)/$width; $width = $Config->{maxwidth}; } return qq{width="$width" height="$height"}; } ## # short_filename($) # sub short_filename($) { my $filename = shift; my $maxlength = 14; ### if leng(ext) == 3 ### # split on last dot (probably extension) my ($file, $ext) = $filename =~ m/(.+)\.(\w+)$/; if (length($ext)>3) { $maxlength = 16 - length($ext); } if (length($filename) > $maxlength) { $file = substr($file, 0, $maxlength)."."; } return $file.'.'.$ext; } ############################################################################# ### do_header() - return XHTML header sub do_header() { my ($last) = fileparse( getcwd() ); $last .= "/".$Config->{'title'} if defined($Config->{'title'}); return < Galeryja - $last
EOF ; } ############################################################################# ### do_footer() - wypisz stopkļæ½ HTML na stdout sub do_footer() { return qq{
Created by galeryja rev $version © eloy
}; } ## # get_file_prevnext sub get_file_prevnext($$) { my ($file, $right) = @_; my $float = 'float: left;'; if ($right == 1) { $float = 'float: right;'; } if ( defined $file ) { $file =~ s/\ /%20/g; return qq {
[$file]
}; } else { return ' '; } } ## # get_file_prevnext sub get_file_thfile($) { my ($path) = @_; my @parts = split ( '/', $path ); my $thfile = ''; foreach my $t (0 .. $#parts-1) { $thfile .= $parts[$t]."/"; } $thfile .= "th_".$parts[$#parts]; return $thfile; } ## # do_file_nav($prev, $next) sub do_file_nav($$$$$) { my ( $counter, $amount, $page, $prev, $next ) = @_; my $p = $page == 0 ? "index.html" : "index-$page.html"; my $index = qq{ index }; if ( defined $next ) { $next =~ s/\ /%20/g; $next = qq{ $next }; } else { $next = ' '; } if ( defined $prev ) { $prev =~ s/\ /%20/g; $prev = qq{ $prev }; } else { $prev = ' '; } return qq {
$index
}; } ############################################################################# ### do_file($image, $counter, $prev, $next) sub do_file($$$$$$) { my ( $image, $counter, $amount, $page, $prev, $next ) = @_; my $imgw = get_info( $image, "width" ); my $imgh = get_info( $image, "height" ); my $size = sprintf( "%d x %d", $imgw, $imgh); ##my $scale = get_scale_image($imgw, $imgh); my $scale = ''; my $thpath = '../'.$image; if ( -f $Config->{thumbdir}.'/th_'.$image ) { $thpath = 'th_'.$image } my $img = qq{$image ($counter/$amount)
[$image]
$size}; open( FFD, ">$Config->{thumbdir}/$image.html" ); print FFD do_header(); print FFD do_file_nav( $counter, $amount, $page, $prev, $next ); print FFD get_file_prevnext($prev, 0); print FFD get_file_prevnext($next, 1); print FFD $img; print FFD qq{
 
}; print FFD do_footer(); close(FFD); } ############################################################################# ### do_index_nav($page, $pages) sub do_index_nav($$) { my ( $page, $pages ) = @_; my $prev = " "; my $next = " "; my $index = ""; if ( $pages == 0 ) { ### do nothing ; } elsif ( $page == 0 ) { my $p = "index-" . $pages . ".html"; my $n = "index-" . ( $page + 1 ) . ".html"; $prev = qq{« ( }; $next = qq{ ) »}; } elsif ( $page == $pages ) { my $p = ($page == 1) ? "index.html" : "index-" . ($page - 1) . ".html"; my $n = "index.html"; $prev = qq{« ( }; $next = qq{ ) »}; } else { my $n = "index-" . ( $page + 1 ) . ".html"; my $p = ($page == 1) ? "index.html" : "index-" . ($page - 1) . ".html"; $prev = qq{« ( }; $next = qq{ ) »}; } for my $p ( 0 .. $pages ) { if ( $p == $pages ) { ## # ostatni indeks if ( $p == $page ) { $index .= sprintf( "%d", $p + 1 ); } else { my $link = $p == 0 ? "index.html" : "index-$p.html"; $index .= sprintf( "%d", $link, $p + 1 ); } } else { ## # other indexes if ( $p == $page ) { $index .= sprintf( "%d : ", $p + 1 ); } else { my $link = $p == 0 ? "index.html" : "index-$p.html"; $index .= sprintf( "%d : ", $link, $p + 1 ); } } } return qq {
Up... $prev $index $next
}; } ## # do_indexes($images) - create index.html # sub do_indexes($) { my $images = shift; ## # how many images my $amount = scalar @{$images}; my $howmany = int( $amount / $Config->{pictures} ); $howmany-- unless ($amount % $Config->{pictures}) ; printf ("Photos %d, pages %d\n", $amount, $howmany+1 ); if ( -d $Config->{thumbdir} ) { print qq{Directory $Config->{thumbdir} exist. Use --clean option to allow overwritting.\n}; exit(1); } mkdir( $Config->{thumbdir} ); for my $page ( 0 .. $howmany ) { my $filename = $page == 0 ? "index.html" : "index-" . $page . ".html"; my @pictures = @$images; my $picsfrom = ( $page * ( $Config->{pictures} ) ); @pictures = splice( @pictures, $picsfrom, $Config->{pictures} ); my $write = 1; if ( -f $filename ) { print qq{File $filename exist. Use --clean option to allow overwritting.\n}; exit(1); } if ( $write ) { open( FD, ">", $filename ); print FD do_header(); print FD do_index_nav( $page, $howmany ); print FD qq{
 
\n}; print FD qq{
\n}; my $counter = 1; foreach my $image (@pictures) { ## # do thumbnails print FD do_miniature($image); ## # navigation my $pict = ( $picsfrom + $counter - 1 ); my $prev = $images->[ $pict - 1 ] || undef; my $next = $images->[ $pict + 1 ] || $images->[0]; do_file( $image, $pict + 1, $amount, $page, $prev, $next ); $counter++; } print FD qq{
\n}; print FD qq{
 
\n}; print FD do_footer(); close(FD); printf( "Generated index no %d.\n", $page + 1 ); } } } ## # remove old files (index.html & thumbs directory # sub do_clean() { my @indexes = ; unlink @indexes; eval ### catch errors { rmtree( $Config->{thumbdir}, 0, 1); }; } ## # main # umask 022; my $help = 0; $|++; GetOptions ( "minx=i" => \$Config->{'minx'}, "miny=i" => \$Config->{'miny'}, "h|help" => \$help, "types=s" => \@{ $Config->{'types'} }, "bgcolor=s" => \$Config->{'bgcolor'}, "textcolor=s" => \$Config->{'textcolor'}, "thumbdir=s" => \$Config->{'thumbdir'}, "clean|c" => \$Config->{'clean'}, "linkcolor=s" => \$Config->{'linkcolor'}, "maxwidth=i" => \$Config->{'maxwidth'}, "maxheight=i" => \$Config->{'maxheight'}, "title=s" => \$Config->{'title'}, "pictures=i" => \$Config->{'pictures'} ); if ($help) { do_help(); exit(0); } if ($Config->{clean}) { ## # first clean old files # do_clean(); } my $files = get_files(); do_indexes($files);