# 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 "<b>img thumbnail read: " . $img->errstr . "</b>";
#		or return "<b>THUMBNAIL NOT FOUND: $img_filename</b>";

  $tag  = "<a href=\"$img::url/$img_filename\"><img";
  $tag .= " src=\"" . thumbnail_url($img_filename ) . "\"";
  $tag .= " width=\"" . $img->getwidth() . "\"";
  $tag .= " height=\"" . $img->getheight() . "\"";
  $tag .= " class=\"" . $class . "\"" if $class;
  $tag .= " style=\"" . $style . "\"";
  $tag .= " alt=\"" . $alt . "\"";
  $tag .= " />";
  $tag .= "</a>";

  return $tag;
}

# Call this from your entry
sub img {
  my ($content, $attributes) = @_;

  return "<b>img::img: must provide 'src'</b>" 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 "<b>img file read error:\n" . $img->errstr . "</b>";

    # Rotate if asked to.
    if ( $rotate ) {
      $img = $img->rotate(right=>$rotate);
    }

# "<b>IMAGE NOT FOUND: $file</b>";

    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 "<b>img thumbnail write error:\n" . $thumb->errstr . "</b>";
	last SAVE;
      }
    }
  }

  # Add CSS voodoo.
  my $tag;

  $tag .= "<div class=\"figure\">\n"
       . "<p>" . tag( $img_filename, $alt, $class, $style ) . "</p>";
  $tag .= "<p>\n$caption</p>" if $caption;
  $tag .= "</div>\n";

  return $tag;
}

1;

__END__

=head1 NAME

Blosxom Plug-in: img

=head1 DESCRIPTION

Use with interpolate_fancy to expand "<img.img src="IMG" style="STYLE" />" tags.

=head1 AUTHOR

Peter Gammie <peteg42 at gmail dot com>

=head1 LICENSE

This source is submitted to the public domain.
Feel free to use and modify it.

THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY WARRANTY OF ANY KIND.
USE AT YOUR OWN RISK!
