blib/lib/Labyrinth/DIUtils/GD.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 SCAR 0.1
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Labyrinth::DIUtils::GD;
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::GD - Digital Image utilities driver with GD for Labyrinth Framework.
11  
12 =head1 SYNOPSIS
13  
14 use Labyrinth::DIUtils::GD;
15  
16 Labyrinth::DIUtils::Tool('GD');
17  
18 my $hook = Labyrinth::DIUtils::GD->new($file);
19 my $hook = $hook->rotate($degrees); # 90, 180, 270
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 GD 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 GD;
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 my $i = GD::Image->newFromJpeg($image) ;
63 die "object image error: [$image]" if !$i;
64  
65 my $atts = {
66 'image' => $image,
67 'object' => $i,
68 };
69  
70 # create the object
71 bless $atts, $self;
72 return $atts;
73 }
74  
75 =head2 Image Manipulation
76  
77 =over 4
78  
79 =item rotate($degrees)
80  
81 Object Method. Passed a single mandatory argument, which is then used to turn
82 the image file the number of degrees specified.
83  
84 Note that GD doesn't support rotating angles other than 90, 180 and 270.
85  
86 =cut
87  
88 sub rotate {
89 my $self = shift;
90 my $degs = shift || return;
91  
92 return unless($self->{image} && $self->{object});
93  
94 my $i = $self->{object};
95  
96 my $p;
97 $p = $i->copyRotate90() if($degs == 90);
98 $p = $i->copyRotate180() if($degs == 180);
99 $p = $i->copyRotate270() if($degs == 270);
100 return unless($p);
101  
102 _writeimage($self->{image},$p->jpeg);
103 $self->{object} = $p;
104 return 1;
105 }
106  
107 =item reduce($xmax,$ymax)
108  
109 Object Method. Passed a two arguments (defaulting to 100x100), which is then
110 used to reduce the image to a size that fit inside a box of the specified
111 dimensions.
112  
113 =cut
114  
115 sub reduce {
116 my $self = shift;
117 my $xmax = shift || 100;
118 my $ymax = shift || 100;
119  
120 return unless($self->{image} && $self->{object});
121  
122 my $i = $self->{object};
123  
124 my ($w,$h);
125 my ($width,$height) = $i->getBounds();
126 return unless($width > $xmax || $height > $ymax);
127  
128 my $x = ($xmax / $width);
129 my $y = ($ymax / $height);
130  
131 if($x < $y) {
132 $w = $xmax;
133 $h = $height * $x;
134 } else {
135 $h = $ymax;
136 $w = $width * $y;
137 }
138  
139 my $p = GD::Image->new($w,$h);
140 $p->copyResized($i,0,0,0,0,$w,$h,$width,$height);
141 _writeimage($self->{image},$p->png);
142 return 1;
143 }
144  
145 =item thumb($thumbnail,$square)
146  
147 Object Method. Passed two arguments, the first being the name of the thumbnail
148 file to be created, and the second being a single dimension of the square box
149 (defaulting to 100), which is then used to reduce the image to a thumbnail.
150  
151 =back
152  
153 =cut
154  
155 sub thumb {
156 my $self = shift;
157 my $file = shift || return;
158 my $smax = shift || 100;
159  
160 my $i = $self->{object};
161 return unless($i);
162  
163 my ($w,$h);
164 my ($width,$height) = $i->getBounds();
165 if($width > $height) {
166 $w = $smax;
167 $h = ($height * $smax) / $width;
168 } else {
169 $h = $smax;
170 $w = ($width * $smax) / $height;
171 }
172  
173 my $p = GD::Image->new($w,$h);
174 $p->copyResized($i,0,0,0,0,$w,$h,$width,$height);
175 _writeimage($file,$p->png);
176 return 1;
177 }
178  
179 sub _writeimage {
180 my ($file,$data) = @_;
181  
182 open IMAGE, ">$file" || die "Cannot write to file [$file]: $!";
183 binmode IMAGE;
184 print IMAGE $data;
185 close IMAGE;
186 }
187  
188 1;
189  
190 __END__
↑ Summary