package Image::Gallery::Thumb;
################################################################################
# $Id: Thumb.pm,v 1.15 2006/04/28 02:53:32 dleonard Exp $
# $Date: 2006/04/28 02:53:32 $
#
# Thumbnail generating library for Image::Gallery
#
# GPL v2
# (c) 2006 dleonard@dleonard.net
################################################################################
use strict;

use Image::Magick;
use Exporter;

use Image::Gallery::Common;

use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
use vars qw(@CONST);

@ISA = qw(Exporter Image::Gallery::Common);

use constant PREPEND => '.thumb_';

@EXPORT_OK = qw(PREPEND);
%EXPORT_TAGS = (all => @EXPORT_OK);

my %default = (width => 150,
               quality => 75);

################################################################################
# new
# I: $options->{Image}   # Image::Magick object
#              {image}   # Path to source image
#              {prepend} # What to prepend to the filename
################################################################################
sub new {
 my ($class, $options) = @_;
 if (! ref $options) {
  shift;
  $options = scalar @_ ? {@_} : {};
 }

 my $self = {Image => undef,
             image => undef,
             prepend => &PREPEND};

 foreach (keys %$self) {
  $self->{$_} = $options->{$_} if exists $options->{$_};
 }

 # Create Image if it wasn't passed in
 if (!$self->{Image}) {
  $self->{Image} = Image::Magick->new();
  $class->fatal('Unable to create new Image::Magick object.') if !$self->{Image};
 }

 $class->fatal($_) if $self->{Image}->Read($self->{image});

 # Get a few attributes needed in a number of places
 ($self->{width}, $self->{height}, $self->{size}, $self->{format}) = $self->{Image}->Ping($self->{image});

 return bless $self, $class;
} #new

################################################################################
# scale
#  Resize the image
# I: $options->{percentage} # Integer percentage
#              {width}      # Width in pixels
################################################################################
sub scale {
 my ($self, $options) = @_;
 if (!ref $options) {
  shift;
  $options = scalar @_ ? {@_} : {};
 }

 my $width = $options->{width} || $default{width};

 if ($options->{percentage}) {
  $width = $self->{width} * $options->{percentage} / 100;
 }

 my $height = int($width / $self->{width} * $self->{height});

 if ($self->{Image}->Scale(width => $width,
                           height => $height)) {
  $self->fatal($_);
 }
} #scale

################################################################################
# write
# I: $options->{format}      # Valid image extension
#              {quality}     # 0..100
################################################################################
sub write {
 my ($self, $options) = @_;
 if (!ref $options) {
  shift;
  $options = scalar @_ ? {@_} : {};
 }

 my $quality = $options->{quality} || $default{quality};

 my $dir;
 ($dir = $self->{image}) =~ s#([^/]+\.)([^/]+)$##;
 $self->{thumb} = $dir . $self->{prepend} . $1;
 if ($options->{format}) {
  $self->{thumb} .= $options->{format};
 } else {
  $self->{thumb} .= $2;
 }

 # Quality is 0 to 100 for jpeg (0 is worst)
 my $return =  $self->{Image}->Write(filename => $self->{thumb},
                                     quality => $quality);

 $self->fatal($return) if $return;
} #write

sub DESTROY {}

1;

__END__

=head1 NAME

Image::Gallery::Thumb - Web image gallery thumbnail generator

=head1 SYNOPSIS

 use Image::Gallery::Thumb

=head1 ABSTRACT

This module provides functionality to create thumbnail files for images in a specified directory.

=head1 DESCRIPTION

This module provides the ability to create thumbnails of specified size, type, and detail level for images in a specified directory.  Additionally it can recurse through a directory structure creating thumbnails at each level.  This module i commonly included by Image::Gallery.pm but can be used stand-alone.

=head1 METHODS
