# Blosxom Plugin: makegallery # Author: Bob Schumaker # Version: 0+3i # http://www.cobblers.net/blog/ # License: Public Domain # dothumb based on code in bbgallery Copyright (C) 2001 Bodo Bauer # # generate thumbs for all gallery stories package makegallery; use Data::Dumper; my $image_size; BEGIN { if(eval "require Image::Size") { Image::Size->import(); $image_size = 1; } } # --- Plug-in package variables ----- # The path to the images (should be web accessible). 'path' will be added to find images $imagedir; # Thumbnail conversion method (-sample vs. -scale) $converthow = "-scale"; # Thumbnail size $thumbsize = 100; # Thumbnail quality $thumbqual = 100; # -------------------------------- my $updated_cache = 0; my $identify; my $convert; my %pictures = (); sub start { $identify = findsysbin( "identify" ) unless( $image_size ); $convert = findsysbin( "convert" ); 1; } sub head { my($pkg, $path, $head_ref) = @_; for (;;) { do { -r "$blosxom::datadir/$path/config.gallery" and require "$blosxom::datadir/$path/config.gallery" and last; } while ($path =~ s/(\/*[^\/]*)$// and $1); last; } $imagedir ||= $showgallery::imagedir; $imagedir ||= $blosxom::datadir; if( defined($hostinfo::do_replacement) ) { $imagedir = &{$hostinfo::do_replacement}($imagedir); } if ( open PICTURES, "$blosxom::plugin_state_dir/$showgallery::gallery_cache" ) { my $index = join '', ; close PICTURES; $index =~ /\$VAR1 = \{/ and eval($index) and !$@ and %pictures = %$VAR1; } return 1; } sub story { my($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_; my $tag = $showgallery::gallery_on_flag; $$body_ref =~ /$tag/ or return 0; # Okay, the images will be in $imagedir/path, and will need a URL of $imageurl/path my @files=glob $imagedir.$path."/*.{jpg,jpeg,png,gif,tiff}"; if( @files ) { mkdir $thumbdir; foreach my $image_file ( @files ) { my $basename = $image_file; $basename =~ s/.*\///; findsize($image_file, $path, $showgallery::thumbprefix . $basename); } } return 1; } sub end { if( $updated_cache ) { if ( open PICTURES, "> $blosxom::plugin_state_dir/$showgallery::gallery_cache" ) { print PICTURES Dumper \%pictures; close PICTURES; } else { warn "couldn't > $blosxom::plugin_state_dir/$showgallery::gallery_cache: $!\n"; } } 1; } sub findsize { my($image_file, $path, $thumbname) = @_; my ($w, $h) = getsize( $image_file ); my $thumbdir = "$imagedir$path/$showgallery::thumbdirname"; my $thumbnail = "$thumbdir/$thumbname"; unless ( -f $thumbnail ) { mkdir $thumbdir; ($w, $h) = dothumb( $image_file, $w, $h, $thumbnail ); $updated_cache = 1; } else { ($w, $h) = getsize( $thumbnail ); } $pictures{$thumbnail} = [ $w, $h ]; return ($w, $h); } sub dothumb { my($src_img, $w, $h, $dst_img, $size, $quality) = @_; return unless defined($convert); my $nh; my $nw; if ( $h && $w ) { $size ||= $thumbsize; $quality ||= $thumbqual; if ( $h>$w ) { $nh = $size; $nw = int ( ( $size/$h ) * $w ); } else { $nw = $size; $nh = int ( ( $size/$w ) * $h ); } system ( "$convert $src_img -quality $quality $converthow $nw" . "x" . "$nh $dst_img" ); return ($nw, $nh); } } sub getsize { my($src_img) = @_; return unless defined($image_size) or defined($identify); my($w, $h) = @{$pictures{$src_img}}; unless( $w && $h ) { if( defined($image_size) && $image_size ) { ($w, $h) = imgsize($src_img); } else { open ( ID, "$identify -format \"%w %h\" $src_img|" ) || die ( "identify not found ($!)\n" ); my @output = ; chomp @output; ($w, $h ) = split (/\s+/, $output[0] , 2); close ( ID ); } $pictures{$src_img} = [ $w, $h ]; $updated_cache = 1; } return @{$pictures{$src_img}}; } sub findsysbin { my($app) = @_; my @dirs = ( "/usr/bin", "/usr/local/bin" ); foreach my $dir (@dirs) { return "$dir/$app" if( -x "$dir/$app" ); } return undef; } 1; __END__ =head1 NAME Blosxom Plug-in: makegallery =head1 SYNOPSIS Purpose: generate the thumbnails for a gallery story For every story being displayed, this plugin will find all of the images in the path and cache their sizes, and generate the thumbnails if required (and the 'convert' command is found). The plugin will use Image::Size if it is available, otherwise the 'identify' is also required. =head1 VERSION 0+3i =head1 AUTHOR Bob Schumaker , http://www.cobblers.net/blog/ =head1 SEE ALSO Blosxom Home/Docs/Licensing: http://www.raelity.org/apps/blosxom/ Blosxom Plugin Docs: http://www.raelity.org/apps/blosxom/plugin.shtml =head1 BUGS Address bug reports and comments to the Blosxom mailing list [http://www.yahoogroups.com/group/blosxom]. =head1 LICENSE Blosxom Copyright 2003, Rael Dornfest This Blosxom Plug-in Copyright 2003, Bob Schumaker Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.