File Coverage

blib/lib/Labyrinth/DIUtils/ImageMagick.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Labyrinth::DIUtils::ImageMagick;
2              
3 6     6   162304 use warnings;
  6         15  
  6         232  
4 6     6   34 use strict;
  6         12  
  6         493  
5              
6             our $VERSION = '5.06';
7              
8             =head1 NAME
9              
10             Labyrinth::DIUtils::ImageMagick - Digital Image utilities driver for ImageMagick.
11              
12             =head1 SYNOPSIS
13              
14             use Labyrinth::DIUtils::ImageMagick;
15              
16             Labyrinth::DIUtils::Tool('ImageMagick');
17              
18             my $hook = Labyrinth::DIUtils::ImageMagick->new($file);
19             my $hook = $hook->rotate($degrees); # 0 - 360
20             my $hook = $hook->reduce($xmax,$ymax);
21             my $hook = $hook->thumb($thumbnail,$square);
22              
23             =head1 DESCRIPTION
24              
25             Handles the driver software for ImageMagick image manipulation; Do not use
26             this module directly, access via Labyrinth::DIUtils.
27              
28             =cut
29              
30             #############################################################################
31             #Modules/External Subroutines #
32             #############################################################################
33              
34 6     6   7846 use Image::Magick;
  0            
  0            
35              
36             #############################################################################
37             #Subroutines
38             #############################################################################
39              
40             =head1 METHODS
41              
42             =head2 Contructor
43              
44             =over 4
45              
46             =item new($file)
47              
48             The constructor. Passed a single mandatory argument, which is then used as the
49             image file for all image manipulation.
50              
51             =back
52              
53             =cut
54              
55             sub new {
56             my $self = shift;
57             my $image = shift;
58              
59             die "no image specified" if !$image;
60             die "no image file found" if !-f $image;
61              
62             # read in current image
63             my $i = Image::Magick->new();
64             die "object image error: [$image]\n" if !$i;
65             my $c = $i->Read($image);
66             die "read image error: [$image] $c\n" if $c;
67              
68             my $atts = {
69             'image' => $image,
70             'object' => $i,
71             };
72              
73             # create the object
74             bless $atts, $self;
75             return $atts;
76             }
77              
78              
79             =head2 Image Manipulation
80              
81             =over 4
82              
83             =item rotate($degrees)
84              
85             Object Method. Passed a single mandatory argument, which is then used to turn
86             the image file the number of degrees specified.
87              
88             =cut
89              
90             sub rotate {
91             my $self = shift;
92             my $degs = shift || return undef;
93              
94             return unless($self->{image} && $self->{object});
95              
96             my $i = $self->{object};
97             $i->Rotate(degrees => $degs);
98             my $c = $i->Write($self->{image});
99             die "write image error: [$self->{image}] $c\n" if $c;
100              
101             return 1;
102             }
103              
104             =item reduce($xmax,$ymax)
105              
106             Object Method. Passed two arguments (defaulting to 100x100), which is then
107             used to reduce the image to a size that fit inside a box of the specified
108             dimensions.
109              
110             =cut
111              
112             sub reduce {
113             my $self = shift;
114             my $xmax = shift || 100;
115             my $ymax = shift || 100;
116              
117             return unless($self->{image} && $self->{object});
118              
119             my $i = $self->{object};
120             my ($width,$height) = $i->Get('columns', 'rows');
121             return unless($width > $xmax || $height > $ymax);
122              
123             $i->Scale(geometry => "${xmax}x${ymax}");
124             my $c = $i->Write($self->{image});
125             die "write image error: [$self->{image}] $c\n" if $c;
126              
127             return 1;
128             }
129              
130             =item thumb($thumbnail,$square)
131              
132             Object Method. Passed two arguments, the first being the name of the thumbnail
133             file to be created, and the second being a single dimension of the square box
134             (defaulting to 100), which is then used to reduce the image to a thumbnail.
135              
136             =back
137              
138             =cut
139              
140             sub thumb {
141             my $self = shift;
142             my $file = shift || return;
143             my $smax = shift || 100;
144              
145             my $i = $self->{object};
146             return unless($i);
147              
148             $i->Scale(geometry => "${smax}x${smax}");
149             my $c = $i->Write($file);
150             die "write image error: [$self->{image}] $c\n" if $c;
151              
152             return 1;
153             }
154              
155             1;
156              
157             __END__