Subject: | Rudimentary Support for Imager |
This is a rough patch to allow Image::Thumbnail to use Imager via module => 'Imager' to create thumbnails.
--- Thumbnail.pm.orig Wed Dec 14 03:34:28 2005
+++ Thumbnail.pm Wed Dec 14 15:41:53 2005
@@ -7,14 +7,14 @@
=head1 NAME
-Image::Thumbnail - simple thumbnails with GD/ImageMagick
+Image::Thumbnail - simple thumbnails with GD/ImageMagick/Imager
=head1 SYNOPSIS
use Image::Thumbnail;
# Create a thumbnail from 'test.jpg' as 'test_t.jpg'
- # using ImageMagick, or GD.
+ # using ImageMagick, Imager, or GD.
my $t = new Image::Thumbnail(
size => 55,
create => 1,
@@ -40,6 +40,16 @@
outputpath => 'test_t.jpg',
);
+ # Create a thumbnail from 'test.jpg' as 'test_t.jpg'
+ # using Imager.
+ my $t = new Image::Thumbnail(
+ module => 'Imager',
+ size => 55,
+ create => 1,
+ inputpath => 'test.jpg',
+ outputpath => 'test_t.jpg',
+ );
+
# Create a thumbnail as 'test_t.jpg' from an ImageMagick object
# using ImageMagick, or GD.
my $t = new Image::Thumbnail(
@@ -60,7 +70,7 @@
=head1 DESCRIPTION
This module allows you to easily make thumbnail images from
-files or objects, using either the ImageMagick or GD library.
+files or objects, using either the ImageMagick, Imager or GD library.
Thumbnails can be created, and either saved as image files or
accessed as objects: see L<create>.
@@ -71,7 +81,7 @@
=head1 PREREQUISITES
-C<Image::Magick> or C<GD>.
+C<Image::Magick>, C<Imager> or C<GD>.
=head1 CONSTRUCTOR new
@@ -112,12 +122,12 @@
=over 4
-=item module ( GD | ImageMagick )
+=item module ( GD | ImageMagick | Imager )
If you wish to use a specific module, place its name here.
You must have the module you require already installed!
-Supplying no name will allow ImageMagick to be tried before GD.
+Supplying no name will allow ImageMagick, then Imager to be tried before GD.
=item create
@@ -154,7 +164,7 @@
=item quality
-ImageMagick only: an integer from 1 to 100 to specify
+ImageMagick/Imager only: an integer from 1 to 100 to specify
the thumbnail quality. Defaults to 50.
=item attr
@@ -290,7 +300,7 @@
$self->{module} = $try;
}
elsif (not $self->{module} and not $try){
- for ('Image::Magick','GD'){
+ for ('Image::Magick','Imager', 'GD'){
if (not $self->set_mod($_)){
return undef;
} else {
@@ -322,7 +332,18 @@
}
warn "Image::Magick OK" if $self->{CHAT};
}
- else {
+ elsif ($self->{module} eq 'Imager'){
+ warn "Requiring Imager" if $self->{CHAT};
+ # eval ('use Imager');
+ require Imager;
+ import Imager;
+ if ($@){
+ warn "Error requring Imager:\n".$@;
+ return undef;
+ }
+ warn "Imager OK" if $self->{CHAT};
+ }
+ else {
warn "Unsupported module $self->{module}" if $self->{CHAT};
return undef;
}
@@ -367,7 +388,9 @@
$r = $self->create_imagemagick;
} elsif ($self->{module} =~ /^(GD|GD::Image)$/){
$r = $self->create_gd;
- } else {
+ } elsif ($self->{module} eq 'Imager'){
+ $r = $self->create_imager;
+ } else {
$self->{error} = "User supplied unknown module ".$self->{module};
warn $self->{error};
$r = undef;
@@ -386,7 +409,7 @@
$self->{object} = Image::Magick->new;
$self->{error} = $self->{object}->Read($self->{inputpath});
if ($self->{error}){
- warn "# ".$self->{error} if $self->{chat};
+ warn "# ".$self->{error} if $self->{CHAT};
return undef;
}
}
@@ -467,6 +490,36 @@
return 1;
}
+#
+# METHOD create_imager
+#
+sub create_imager { my $self=shift;
+ warn "...with Imager" if $self->{CHAT};
+ if (not $self->{object} or ref $self->{object} ne 'Imager'){
+ warn "...from file $self->{inputpath}" if $self->{CHAT};
+ $self->{object} = Imager->new;
+ eval {$self->{object}->read(file => $self->{inputpath})};
+
+ if ($@){
+ warn "# $@" if $self->{CHAT};
+ return undef;
+ }
+ }
+ return undef unless $self->imager_thumb;
+
+ if ($self->{outputpath}){
+ warn "Writing to $self->{outputpath}" if $self->{CHAT};
+ eval {$self->{object}->write(
+ file => $self->{outputpath}, jpegquality=>($self->{quality}||'50')
+ )};
+ if ($@) {
+ warn "# $@" if $self->{CHAT};
+ return undef;
+ };
+ }
+ warn "Done Imager: ",$self->{x}," ",$self->{y} if $self->{CHAT};
+ return 1;
+}
#
@@ -503,7 +556,7 @@
}
#
-# Thumbnail generatio
+# Thumbnail generation
#
sub gd_thumb { my $self=shift;
if (not $self->{object}){
@@ -556,6 +609,31 @@
}
}
return 1; # Thanks, Himmy :)
+}
+
+
+sub imager_thumb { my $self=shift;
+ if (not $self->{object}){
+ $self->{error} = "No 'object' supplied to make thumbnail from";
+ warn $self->{error} if $self->{CHAT};
+ return undef;
+ }
+ $self->{ox} = $self->{object}->getwidth;
+ $self->{oy} = $self->{object}->getheight;
+
+ if (not $self->{ox} or not $self->{oy}){
+ $self->{error} = __PACKAGE__." Could not get width/height from image";
+ warn $self->{error} if $self->{CHAT};
+ return undef
+ }
+
+ $self->_size;
+ $self->{x} = int( $self->{ox}/$self->{ratio} );
+ $self->{y} = int( $self->{oy}/$self->{ratio} );
+
+ $self->{object} = $self->{object}->scale(xpixels=>$self->{x},ypixels=>$self->{y},type=>'min');
+
+ return 1;
}