| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package GD::RPPNG; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Author: Pascal Gloor pascal.gloor@spale.com |
|
4
|
|
|
|
|
|
|
# Date: 11 Oct 2003 |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
require 5.004; |
|
7
|
|
|
|
|
|
|
require GD; |
|
8
|
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
613
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
26
|
|
|
10
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
24
|
|
|
11
|
1
|
|
|
1
|
|
1359
|
use GD 2.11; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.9'; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
############# Code Begin |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Turn off buffering |
|
18
|
|
|
|
|
|
|
$|=1; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Definition of the main hash |
|
21
|
|
|
|
|
|
|
my %opt; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Declaration of new object with data types defined and blessed for use |
|
24
|
|
|
|
|
|
|
sub new |
|
25
|
|
|
|
|
|
|
{ |
|
26
|
|
|
|
|
|
|
my $package = shift; |
|
27
|
|
|
|
|
|
|
my $object = { myimage => [] }; |
|
28
|
|
|
|
|
|
|
my $bless = bless($object, $package); |
|
29
|
|
|
|
|
|
|
_defaults($bless); |
|
30
|
|
|
|
|
|
|
return $bless; |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Setting all options to their default value |
|
34
|
|
|
|
|
|
|
sub _defaults |
|
35
|
|
|
|
|
|
|
{ |
|
36
|
|
|
|
|
|
|
my $self = shift; |
|
37
|
|
|
|
|
|
|
$opt{$self}{xsize} = 400; |
|
38
|
|
|
|
|
|
|
$opt{$self}{ysize} = 75; |
|
39
|
|
|
|
|
|
|
$opt{$self}{charset} = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789%$@'; |
|
40
|
|
|
|
|
|
|
$opt{$self}{codelen} = 8; |
|
41
|
|
|
|
|
|
|
$opt{$self}{colors} = 1; |
|
42
|
|
|
|
|
|
|
$opt{$self}{fontminpt} = 12; |
|
43
|
|
|
|
|
|
|
$opt{$self}{fontmaxpt} = 28; |
|
44
|
|
|
|
|
|
|
$opt{$self}{xlinesfactor} = 10; |
|
45
|
|
|
|
|
|
|
$opt{$self}{ylinesfactor} = 10; |
|
46
|
|
|
|
|
|
|
$opt{$self}{ydivert} = 10; |
|
47
|
|
|
|
|
|
|
$opt{$self}{angle} = 45; |
|
48
|
|
|
|
|
|
|
$opt{$self}{transparent} = 0; |
|
49
|
|
|
|
|
|
|
$opt{$self}{debugcode} = 0; |
|
50
|
|
|
|
|
|
|
@{$opt{$self}{bgcolor}} = ( 255, 255, 255 ); |
|
51
|
|
|
|
|
|
|
@{$opt{$self}{fgcolor}} = ( 0, 0, 0 ); |
|
52
|
|
|
|
|
|
|
$opt{$self}{code} = ''; |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
undef @{$opt{$self}{ttf}}; |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# User overriding default options |
|
58
|
|
|
|
|
|
|
sub Config |
|
59
|
|
|
|
|
|
|
{ |
|
60
|
|
|
|
|
|
|
my $self = shift; |
|
61
|
|
|
|
|
|
|
my @options = @_ if (@_); |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
while(@options) |
|
64
|
|
|
|
|
|
|
{ |
|
65
|
|
|
|
|
|
|
my $name = shift(@options); |
|
66
|
|
|
|
|
|
|
$name =~ tr/[A-Z]/[a-z]/; |
|
67
|
|
|
|
|
|
|
my $value = shift(@options); |
|
68
|
|
|
|
|
|
|
if ( $name =~ /bgcolor|fgcolor/ ) |
|
69
|
|
|
|
|
|
|
{ |
|
70
|
|
|
|
|
|
|
$value =~ s/(..)(..)(..)/$1 $2 $3/; |
|
71
|
|
|
|
|
|
|
my ($r,$g,$b) = split(/ /,$value); |
|
72
|
|
|
|
|
|
|
@{$opt{$self}{$name}} = ( hex($r), hex($g), hex($b) ); |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
else |
|
75
|
|
|
|
|
|
|
{ |
|
76
|
|
|
|
|
|
|
$opt{$self}{$name}=$value; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# Adding a TrueType Font |
|
82
|
|
|
|
|
|
|
sub AddFonts |
|
83
|
|
|
|
|
|
|
{ |
|
84
|
|
|
|
|
|
|
my $self = shift; |
|
85
|
|
|
|
|
|
|
my $font = shift; |
|
86
|
|
|
|
|
|
|
if ( -r "$font" ) |
|
87
|
|
|
|
|
|
|
{ |
|
88
|
|
|
|
|
|
|
push(@{$opt{$self}{ttf}},$font); |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
else |
|
91
|
|
|
|
|
|
|
{ |
|
92
|
|
|
|
|
|
|
warn "Could not read TrueType Font \"$font\"\n"; |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# Generating a random password |
|
97
|
|
|
|
|
|
|
sub _gencode |
|
98
|
|
|
|
|
|
|
{ |
|
99
|
|
|
|
|
|
|
my $self = shift; |
|
100
|
|
|
|
|
|
|
my $len = length($opt{$self}{charset}); |
|
101
|
|
|
|
|
|
|
my @charset; |
|
102
|
|
|
|
|
|
|
$opt{$self}{code} = ''; |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
foreach ( split(//,$opt{$self}{charset}) ) |
|
105
|
|
|
|
|
|
|
{ |
|
106
|
|
|
|
|
|
|
push(@charset,$_); |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
for ( 1 .. $opt{$self}{codelen} ) |
|
110
|
|
|
|
|
|
|
{ |
|
111
|
|
|
|
|
|
|
$opt{$self}{code} .= $charset[(int(rand(length($opt{$self}{charset}))))]; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# Generating the image |
|
116
|
|
|
|
|
|
|
sub GenImage |
|
117
|
|
|
|
|
|
|
{ |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
my $self = shift; |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# generate a random password if not set |
|
122
|
|
|
|
|
|
|
if ( !length($opt{$self}{code}) ) |
|
123
|
|
|
|
|
|
|
{ |
|
124
|
|
|
|
|
|
|
_gencode($self); |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
my $fonts = @{$opt{$self}{ttf}}; |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# create base image |
|
130
|
|
|
|
|
|
|
my $image = new GD::Image($opt{$self}{xsize},$opt{$self}{ysize}); |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# setting back/fore-ground colors |
|
133
|
|
|
|
|
|
|
my $fgcolor = $image->colorAllocate(@{$opt{$self}{fgcolor}}); |
|
134
|
|
|
|
|
|
|
my $bgcolor = $image->colorAllocate(@{$opt{$self}{bgcolor}}); |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# setting background as transparent if requested |
|
137
|
|
|
|
|
|
|
if ( $opt{$self}{transparent} ) |
|
138
|
|
|
|
|
|
|
{ |
|
139
|
|
|
|
|
|
|
$image->transparent($bgcolor); |
|
140
|
|
|
|
|
|
|
} |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
my @colors; |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# adding some colors to the palette |
|
145
|
|
|
|
|
|
|
if ( $opt{$self}{colors} ) |
|
146
|
|
|
|
|
|
|
{ |
|
147
|
|
|
|
|
|
|
foreach(1..50) |
|
148
|
|
|
|
|
|
|
{ |
|
149
|
|
|
|
|
|
|
push(@colors,$image->colorAllocate( |
|
150
|
|
|
|
|
|
|
int(rand(200)), |
|
151
|
|
|
|
|
|
|
int(rand(200)), |
|
152
|
|
|
|
|
|
|
int(rand(200)) ) ); |
|
153
|
|
|
|
|
|
|
} |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
# setting the background |
|
157
|
|
|
|
|
|
|
$image->filledRectangle(0,0,$opt{$self}{xsize}-1,$opt{$self}{ysize}-1,$bgcolor); |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
my $ncolors = @colors; |
|
160
|
|
|
|
|
|
|
my $pos = 10; |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# loop on each code char |
|
163
|
|
|
|
|
|
|
foreach(split(//,$opt{$self}{code})) |
|
164
|
|
|
|
|
|
|
{ |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
# setting font color and deviation |
|
167
|
|
|
|
|
|
|
my $color = $fgcolor; |
|
168
|
|
|
|
|
|
|
my $margin = int( $opt{$self}{ysize} * $opt{$self}{ydivert} / 100 ); |
|
169
|
|
|
|
|
|
|
my $dev = int( rand( $margin ) + ( ( $opt{$self}{ysize} - $margin ) / 2 ) ); |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
# re-setting color |
|
172
|
|
|
|
|
|
|
if ( $opt{$self}{colors} ) |
|
173
|
|
|
|
|
|
|
{ |
|
174
|
|
|
|
|
|
|
$color = $colors[int(rand($ncolors-2))+2]; |
|
175
|
|
|
|
|
|
|
} |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
# printing the char |
|
178
|
|
|
|
|
|
|
if ( @{$opt{$self}{ttf}} ) |
|
179
|
|
|
|
|
|
|
{ |
|
180
|
|
|
|
|
|
|
my $pt = int( rand( $opt{$self}{fontmaxpt} - $opt{$self}{fontminpt} ) + $opt{$self}{fontminpt} ); |
|
181
|
|
|
|
|
|
|
my $angle = 0; |
|
182
|
|
|
|
|
|
|
if ( $opt{$self}{angle} ) |
|
183
|
|
|
|
|
|
|
{ |
|
184
|
|
|
|
|
|
|
$angle = sprintf("%0.2f", rand($opt{$self}{angle}/180*3.14) - ( $opt{$self}{angle}/360*3.14) ); |
|
185
|
|
|
|
|
|
|
} |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
$image->stringFT( |
|
188
|
|
|
|
|
|
|
$color, |
|
189
|
|
|
|
|
|
|
@{$opt{$self}{ttf}}[int(rand($fonts))], |
|
190
|
|
|
|
|
|
|
$pt, |
|
191
|
|
|
|
|
|
|
$angle, |
|
192
|
|
|
|
|
|
|
$pos, |
|
193
|
|
|
|
|
|
|
$dev+($pt/2), |
|
194
|
|
|
|
|
|
|
$_ |
|
195
|
|
|
|
|
|
|
); |
|
196
|
|
|
|
|
|
|
} |
|
197
|
|
|
|
|
|
|
else |
|
198
|
|
|
|
|
|
|
{ |
|
199
|
|
|
|
|
|
|
$image->string(gdGiantFont,$pos,$dev,$_,$color); |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# moving the position forward for the next char |
|
203
|
|
|
|
|
|
|
$pos += int($opt{$self}{xsize}/length($opt{$self}{code})); |
|
204
|
|
|
|
|
|
|
} |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
# printing the X lines |
|
208
|
|
|
|
|
|
|
for ( 1 .. ( $opt{$self}{xsize} * $opt{$self}{xlinesfactor} / 100 ) ) |
|
209
|
|
|
|
|
|
|
{ |
|
210
|
|
|
|
|
|
|
my $pos = int( rand($opt{$self}{xsize} ) ); |
|
211
|
|
|
|
|
|
|
my $color = $fgcolor; |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
if ( $opt{$self}{colors} ) |
|
214
|
|
|
|
|
|
|
{ |
|
215
|
|
|
|
|
|
|
$color = $colors[int(rand($ncolors))]; |
|
216
|
|
|
|
|
|
|
} |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
$image->line($pos,0,$pos,$opt{$self}{ysize},$color); |
|
219
|
|
|
|
|
|
|
} |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
# printing the Y lines |
|
222
|
|
|
|
|
|
|
for ( 1 .. ( $opt{$self}{ysize} * $opt{$self}{ylinesfactor} / 100 ) ) |
|
223
|
|
|
|
|
|
|
{ |
|
224
|
|
|
|
|
|
|
my $pos = int( rand($opt{$self}{ysize} ) ); |
|
225
|
|
|
|
|
|
|
my $color = $fgcolor; |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
if ( $opt{$self}{colors} ) |
|
228
|
|
|
|
|
|
|
{ |
|
229
|
|
|
|
|
|
|
$color = $colors[int(rand($ncolors))]; |
|
230
|
|
|
|
|
|
|
} |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
$image->line(0,$pos,$opt{$self}{xsize},$pos,$color); |
|
233
|
|
|
|
|
|
|
} |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
# adding the code to the image if debugcode enabled |
|
236
|
|
|
|
|
|
|
if ( $opt{$self}{debugcode} ) |
|
237
|
|
|
|
|
|
|
{ |
|
238
|
|
|
|
|
|
|
$image->filledRectangle(0,0,length($opt{$self}{code})*11+10,15,$fgcolor); |
|
239
|
|
|
|
|
|
|
$image->string(gdMediumBoldFont,0,0,"Code: $opt{$self}{code}",$bgcolor); |
|
240
|
|
|
|
|
|
|
} |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
# return the code and the generated image |
|
243
|
|
|
|
|
|
|
return ( |
|
244
|
|
|
|
|
|
|
$opt{$self}{code}, |
|
245
|
|
|
|
|
|
|
$image->png, |
|
246
|
|
|
|
|
|
|
); |
|
247
|
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
delete $opt{$self}; |
|
249
|
|
|
|
|
|
|
} |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
############# Code End |
|
252
|
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
1; |
|
254
|
|
|
|
|
|
|
__END__ |