# Blosxom Plugin: img -*- Perl -*-
# Author: Peter Gammie
# Version: 0.1
# Relies on the Imager bundle, which I found easier to install than PerlMagick.
# Shouldn't be too much hassle to switch though.
# FIXME: rely on interpolation, turn this all into a single function.
package img;
# --- Configurable variables ----
# Where is the image source directory?
$dir = "/home/peteg/public_html/static/";
# FIXME Use (my hacked) blosxom's $static_url
$url = "$blosxom::static_url/static";
# Where is the thumbnail cache directory (wrt $dir and $url)?
$cache_dir = "cache";
# "Maxpect" size of thumbnails.
# The maximum x or y (or both, if the image is square) dimension in pixels.
$maxpect = 70;
# -------------------------------
use strict;
use Imager;
sub start {
1;
}
# Abstract all this naming jazz.
sub thumbnail_file {
my ($img_filename) = @_;
return "$img::dir/$img::cache_dir/tn_${img_filename}";
}
sub thumbnail_url {
my ($img_filename) = @_;
return "$img::url/$img::cache_dir/tn_$img_filename";
}
# The HTML tag for the image (thumbnail and hyperref)
sub tag {
my ($img_filename, $alt, $class, $style) = @_;
my $tag;
# Figure out the size of the thumbnail.
# FIXME error
my $img = Imager->new();
$img->read(file => thumbnail_file( $img_filename ) )
or return "img thumbnail read: " . $img->errstr . "";
# or return "THUMBNAIL NOT FOUND: $img_filename";
$tag = "getwidth() . "\"";
$tag .= " height=\"" . $img->getheight() . "\"";
$tag .= " class=\"" . $class . "\"" if $class;
$tag .= " style=\"" . $style . "\"";
$tag .= " alt=\"" . $alt . "\"";
$tag .= " />";
$tag .= "";
return $tag;
}
# Call this from your entry
sub img {
my ($content, $attributes) = @_;
return "img::img: must provide 'src'" if ! $attributes->{'src'};
my $img_filename = $attributes->{'src'};
my $alt = $attributes->{'alt'};
my $rotate = $attributes->{'rotate'};
my $class = $attributes->{'class'} || "scaled";
my $style = $attributes->{'style'};
my $caption = $attributes->{'caption'};
my $file = "$img::dir/$img_filename";
my $cache_file = thumbnail_file( $img_filename );
my $imgtag;
if ( ! -e $cache_file ) {
my $img = Imager->new();
$img->read(file=>$file)
or return "img file read error:\n" . $img->errstr . "";
# Rotate if asked to.
if ( $rotate ) {
$img = $img->rotate(right=>$rotate);
}
# "IMAGE NOT FOUND: $file";
my $thumb = $img->scale( xpixels => $img::maxpect,
ypixels => $img::maxpect );
# FIXME: either just use JPEG or try to follow the input format.
SAVE:
for my $format ( qw( jpeg png gif tiff ppm ) ) {
# Check if given format is supported
if ($Imager::formats{$format}) {
$thumb->write(file=>$cache_file)
or return "img thumbnail write error:\n" . $thumb->errstr . "";
last SAVE;
}
}
}
# Add CSS voodoo.
my $tag;
$tag .= "
" . tag( $img_filename, $alt, $class, $style ) . "
"; $tag .= "\n$caption
" if $caption; $tag .= "