line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Labyrinth::DIUtils::GD; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
80842
|
use warnings; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
185
|
|
4
|
6
|
|
|
6
|
|
25
|
use strict; |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
283
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '5.08'; |
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
|
|
|
6
|
|
4079
|
use GD; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
use IO::File; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
############################################################################# |
38
|
|
|
|
|
|
|
#Subroutines |
39
|
|
|
|
|
|
|
############################################################################# |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 METHODS |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head2 Contructor |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=over 4 |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=item new($file) |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
The constructor. Passed a single mandatory argument, which is then used as the |
50
|
|
|
|
|
|
|
image file for all image manipulation. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=back |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=cut |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub new { |
57
|
|
|
|
|
|
|
my $self = shift; |
58
|
|
|
|
|
|
|
my $image = shift; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
die "no image specified" if !$image; |
61
|
|
|
|
|
|
|
die "no image file found" if !-f $image; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my $i = GD::Image->newFromJpeg($image) ; |
64
|
|
|
|
|
|
|
die "object image error: [$image]" if !$i; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $atts = { |
67
|
|
|
|
|
|
|
'image' => $image, |
68
|
|
|
|
|
|
|
'object' => $i, |
69
|
|
|
|
|
|
|
}; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# create the object |
72
|
|
|
|
|
|
|
bless $atts, $self; |
73
|
|
|
|
|
|
|
return $atts; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 Image Manipulation |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=over 4 |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=item rotate($degrees) |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Object Method. Passed a single mandatory argument, which is then used to turn |
83
|
|
|
|
|
|
|
the image file the number of degrees specified. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Note that GD doesn't support rotating angles other than 90, 180 and 270. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub rotate { |
90
|
|
|
|
|
|
|
my $self = shift; |
91
|
|
|
|
|
|
|
my $degs = shift || return; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
return unless($self->{image} && $self->{object}); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my $i = $self->{object}; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
my $p; |
98
|
|
|
|
|
|
|
$p = $i->copyRotate90() if($degs == 90); |
99
|
|
|
|
|
|
|
$p = $i->copyRotate180() if($degs == 180); |
100
|
|
|
|
|
|
|
$p = $i->copyRotate270() if($degs == 270); |
101
|
|
|
|
|
|
|
return unless($p); |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
_writeimage($self->{image},$p->jpeg); |
104
|
|
|
|
|
|
|
$self->{object} = $p; |
105
|
|
|
|
|
|
|
return 1; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item reduce($xmax,$ymax) |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Object Method. Passed a two arguments (defaulting to 100x100), which is then |
111
|
|
|
|
|
|
|
used to reduce the image to a size that fit inside a box of the specified |
112
|
|
|
|
|
|
|
dimensions. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=cut |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub reduce { |
117
|
|
|
|
|
|
|
my $self = shift; |
118
|
|
|
|
|
|
|
my $xmax = shift || 100; |
119
|
|
|
|
|
|
|
my $ymax = shift || 100; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
return unless($self->{image} && $self->{object}); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
my $i = $self->{object}; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
my ($w,$h); |
126
|
|
|
|
|
|
|
my ($width,$height) = $i->getBounds(); |
127
|
|
|
|
|
|
|
return unless($width > $xmax || $height > $ymax); |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my $x = ($xmax / $width); |
130
|
|
|
|
|
|
|
my $y = ($ymax / $height); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
if($x < $y) { |
133
|
|
|
|
|
|
|
$w = $xmax; |
134
|
|
|
|
|
|
|
$h = $height * $x; |
135
|
|
|
|
|
|
|
} else { |
136
|
|
|
|
|
|
|
$h = $ymax; |
137
|
|
|
|
|
|
|
$w = $width * $y; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
my $p = GD::Image->new($w,$h); |
141
|
|
|
|
|
|
|
$p->copyResized($i,0,0,0,0,$w,$h,$width,$height); |
142
|
|
|
|
|
|
|
_writeimage($self->{image},$p->png); |
143
|
|
|
|
|
|
|
return 1; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item thumb($thumbnail,$square) |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Object Method. Passed two arguments, the first being the name of the thumbnail |
149
|
|
|
|
|
|
|
file to be created, and the second being a single dimension of the square box |
150
|
|
|
|
|
|
|
(defaulting to 100), which is then used to reduce the image to a thumbnail. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=back |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub thumb { |
157
|
|
|
|
|
|
|
my $self = shift; |
158
|
|
|
|
|
|
|
my $file = shift || return; |
159
|
|
|
|
|
|
|
my $smax = shift || 100; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
my $i = $self->{object}; |
162
|
|
|
|
|
|
|
return unless($i); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
my ($w,$h); |
165
|
|
|
|
|
|
|
my ($width,$height) = $i->getBounds(); |
166
|
|
|
|
|
|
|
if($width > $height) { |
167
|
|
|
|
|
|
|
$w = $smax; |
168
|
|
|
|
|
|
|
$h = ($height * $smax) / $width; |
169
|
|
|
|
|
|
|
} else { |
170
|
|
|
|
|
|
|
$h = $smax; |
171
|
|
|
|
|
|
|
$w = ($width * $smax) / $height; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
my $p = GD::Image->new($w,$h); |
175
|
|
|
|
|
|
|
$p->copyResized($i,0,0,0,0,$w,$h,$width,$height); |
176
|
|
|
|
|
|
|
_writeimage($file,$p->png); |
177
|
|
|
|
|
|
|
return 1; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub _writeimage { |
181
|
|
|
|
|
|
|
my ($file,$data) = @_; |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
my $fh = IO::File->new($file,'w+') || die "Cannot write to file [$file]: $!"; |
184
|
|
|
|
|
|
|
$fh->binmode; |
185
|
|
|
|
|
|
|
print $fh $data; |
186
|
|
|
|
|
|
|
$fh->close; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
1; |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
__END__ |