blib/lib/Labyrinth/DIUtils/ImageMagick.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Labyrinth::DIUtils::ImageMagick;
2  
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 our $VERSION = '5.07';
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 ▶use Image::Magick;
Subroutine BEGIN: called CC=1 SCAR=2.6
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;
93  
94 return unless($self->{image} && $self->{object});
95  
96 my $i = $self->{object};
97 $i->Rotate(degrees => $degs);
98 $self->_writeimage($self->{image});
99  
100 return 1;
101 }
102  
103 =item reduce($xmax,$ymax)
104  
105 Object Method. Passed two arguments (defaulting to 100x100), which is then
106 used to reduce the image to a size that fit inside a box of the specified
107 dimensions.
108  
109 =cut
110  
111 sub reduce {
112 my $self = shift;
113 my $xmax = shift || 100;
114 my $ymax = shift || 100;
115 my $qual = shift || 0;
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 # set the quality of the image, if specified
124 $i->Set( quality => $qual ) if($qual);
125  
126 $i->Scale(geometry => "${xmax}x${ymax}");
127 $self->_writeimage($self->{image});
128  
129 return 1;
130 }
131  
132 =item thumb($thumbnail,$square)
133  
134 Object Method. Passed two arguments, the first being the name of the thumbnail
135 file to be created, and the second being a single dimension of the square box
136 (defaulting to 100), which is then used to reduce the image to a thumbnail.
137  
138 =back
139  
140 =cut
141  
142 sub thumb {
143 my $self = shift;
144 my $file = shift || return;
145 my $smax = shift || 100;
146  
147 return unless($self->{object});
148  
149 my $i = $self->{object};
150 $i->Scale(geometry => "${smax}x${smax}");
151 $self->_writeimage($file);
152  
153 return 1;
154 }
155  
156 sub _writeimage {
157 my $self = shift;
158 my $file = shift;
159  
160 my $i = $self->{object};
161 return unless($i);
162  
163 my $c = $i->Write($file);
164 die "write image error: [$self->{image}] $c\n" if $c;
165 }
166  
167 1;
168  
169 __END__
↑ Summary