-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcaptcha
executable file
·375 lines (361 loc) · 14.1 KB
/
captcha
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/bin/bash
#
# Developed by Fred Weinhaus 6/29/2008 .......... revised 4/25/2015
#
# ------------------------------------------------------------------------------
#
# Licensing:
#
# Copyright © Fred Weinhaus
#
# My scripts are available free of charge for non-commercial use, ONLY.
#
# For use of my scripts in commercial (for-profit) environments or
# non-free applications, please contact me (Fred Weinhaus) for
# licensing arrangements. My email address is fmw at alink dot net.
#
# If you: 1) redistribute, 2) incorporate any of these scripts into other
# free applications or 3) reprogram them in another scripting language,
# then you must contact me for permission, especially if the result might
# be used in a commercial or for-profit environment.
#
# My scripts are also subject, in a subordinate manner, to the ImageMagick
# license, which can be found at: http://www.imagemagick.org/script/license.php
#
# ------------------------------------------------------------------------------
#
####
#
# USAGE: captcha [-f font] [-p pointsize] [-t textcolor] [-u undercolor] [-b bordercolor] [-m mode] [-a angle] [-r resize] outfile
# USAGE: captcha [-h or -help]
#
# OPTIONS:
#
# -f font fontname or path-to-font-file; default=TimesNewRoman
# -p pointsize pointsize for font; default=40
# -t textcolor color of text; any IM color specification; default=black
# -u undercolor background color under text; any IM color specification;
# default=white;
# -b bordercolor color of image border; any IM color specification;
# default=black;
# -m mode mode to orient text: skew, rotate, both; default=skew
# -a angle maximum orientation angle of text used with mode;
# default=40 (degrees). Values are integers 0<=angle<=60
# -r resize output image resize in percent; default=100 (percent) which
# produces an image 300x80 pixels
#
###
#
# NAME: CAPTCHA
#
# PURPOSE: To create a six-alphanumeric-character image for use as a challenge
# response test.
#
# DESCRIPTION: CAPTCHA creates a six-alphanumeric-character image for use as a challenge
# response test. It builds a default image of size 300x80 pixels containing 6 alphanumeric
# characters that are randomly positioned and oriented and overlaid with random curved and
# straight lines segments.
#
#
# ARGUMENTS:
#
# -f font ... FONT is the desired font or path-to-font-file for the text. The default
# is TimesNewRoman.
#
# -p pointsize ... POINTSIZE is the desired pointsize for the font. The default is 40.
#
# -t textcolor ... TEXTCOLOR is the color to apply to the text. Any valid IM
# text color may be used. The default is black.
# See http://imagemagick.org/script/color.php
#
# -u undercolor ... UNDERCOLOR is the background color to apply directly under the text.
# Any valid IM text color may be used. The default is white.
# See http://imagemagick.org/script/color.php
#
# -b bordercolor ... BORDERCOLOR is the color to apply to the image border.
# Any valid IM text color may be used. The default is black.
# See http://imagemagick.org/script/color.php
#
# -m mode ... MODE is the mode for randomly orienting the text. The choices are:
# skew, rotate or both.
#
# -a angle ... ANGLE is the maximum angle to use to randomly orient the text.
# The default=40 (degrees).
#
# -r resize ... RESIZE is the percentage of the default output image size of 300x80
# to use to generate the output image. The default is 100 (percent). All images are
# created at a size of 300x80, including a 5 pixel border. If resize is specified,
# the output image will be resized accordingly by a post-processing step using
# the IM -resize function.
#
# NOTE: Some effects require the use of -fx and thus will be slower than the others.
# These include: the variations on bulge, concave, convex and pinch.
#
# CAVEAT: No guarantee that this script will work on all platforms,
# nor that trapping of inconsistent parameters is complete and
# foolproof. Use At Your Own Risk.
#
######
#
# set default values
mode="skew"
angle="40"
font="TimesNewRoman"
pointsize="40"
textcolor="black"
bordercolor="black"
undercolor="white"
resize=100
# set directory for temporary files
dir="." # suggestions are dir="." or dir="/tmp"
# set up functions to report Usage and Usage with Description
PROGNAME=`type $0 | awk '{print $3}'` # search for executable on path
PROGDIR=`dirname $PROGNAME` # extract directory of program
PROGNAME=`basename $PROGNAME` # base name of program
usage1()
{
echo >&2 ""
echo >&2 "$PROGNAME:" "$@"
sed >&2 -e '1,/^####/d; /^###/g; /^#/!q; s/^#//; s/^ //; 4,$p' "$PROGDIR/$PROGNAME"
}
usage2()
{
echo >&2 ""
echo >&2 "$PROGNAME:" "$@"
sed >&2 -e '1,/^####/d; /^######/g; /^#/!q; s/^#*//; s/^ //; 4,$p' "$PROGDIR/$PROGNAME"
}
# function to report error messages
errMsg()
{
echo ""
echo $1
echo ""
usage1
exit 1
}
# function to test for minus at start of value of second part of option 1 or 2
checkMinus()
{
test=`echo "$1" | grep -c '^-.*$'` # returns 1 if match; 0 otherwise
[ $test -eq 1 ] && errMsg "$errorMsg"
}
# test for correct number of arguments and get values
if [ $# -eq 0 ]
then
# help information
echo ""
usage2
exit 0
elif [ $# -gt 17 ]
then
errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---"
else
while [ $# -gt 0 ]
do
# get parameter values
case "$1" in
-h|-help) # help information
echo ""
usage2
exit 0
;;
-f) # get font
shift # to get the next parameter
# test if parameter starts with minus sign
errorMsg="--- INVALID FONT SPECIFICATION ---"
checkMinus "$1"
font="$1"
;;
-p) # get pointsize
shift # to get the next parameter
# test if parameter starts with minus sign
errorMsg="--- INVALID POINTSIZE SPECIFICATION ---"
checkMinus "$1"
pointsize=`expr "$1" : '\([0-9]*\)'`
[ "$pointsize" = "" ] && errMsg "--- POINTSIZE=$pointsize MUST BE A NON-NEGATIVE INTEGER ---"
pointsizetestA=`echo "$pointsize <= 0" | bc`
[ $pointsizetestA -eq 1 ] && errMsg "--- POINTSIZE=$pointsize MUST BE AN INTEGER GREATER THAN 0 ---"
;;
-t) # get textcolor
shift # to get the next parameter
# test if parameter starts with minus sign
errorMsg="--- INVALID TEXTCOLOR SPECIFICATION ---"
checkMinus "$1"
textcolor="$1"
;;
-u) # get undercolor
shift # to get the next parameter
# test if parameter starts with minus sign
errorMsg="--- INVALID UNDERCOLOR SPECIFICATION ---"
checkMinus "$1"
undercolor="$1"
;;
-b) # get bordercolor
shift # to get the next parameter
# test if parameter starts with minus sign
errorMsg="--- INVALID BORDERCOLOR SPECIFICATION ---"
checkMinus "$1"
bordercolor="$1"
;;
-m) # get mode
shift # to get the next parameter
# test if parameter starts with minus sign
errorMsg="--- INVALID MODE SPECIFICATION ---"
checkMinus "$1"
mode="$1"
#convert mode to lowercase
mode=`echo "$mode" | tr "[:upper:]" "[:lower:]"`
case "$mode" in
skew) ;;
rotate) ;;
both) ;;
*) errMsg "--- MODE=$mode IS AN INVALID VALUE ---"
esac
;;
-a) # get angle
shift # to get the next parameter
# test if parameter starts with minus sign
errorMsg="--- INVALID ANGLE SPECIFICATION ---"
checkMinus "$1"
angle=`expr "$1" : '\([0-9]*\)'`
[ "$angle" = "" ] && errMsg "--- angle=$angle MUST BE A NON-NEGATIVE INTEGER ---"
angletestA=`echo "$angle < 0" | bc`
angletestB=`echo "$angle > 60" | bc`
[ $angletestA -eq 1 -o $angletestB -eq 1 ] && errMsg "--- angle=$angle MUST BE AN INTEGER BETWEEN 0 AND 60 ---"
;;
-r) # get resize
shift # to get the next parameter
# test if parameter starts with minus sign
errorMsg="--- INVALID RESIZE SPECIFICATION ---"
checkMinus "$1"
resize=`expr "$1" : '\([0-9]*\)'`
[ "$resize" = "" ] && errMsg "--- RESIZE=$resize MUST BE A NON-NEGATIVE INTEGER ---"
resizetestA=`echo "$resize <= 0" | bc`
[ $resizetestA -eq 1 ] && errMsg "--- RESIZE=$resize MUST BE AN INTEGER GREATER THAN 0 ---"
;;
-) # STDIN and end of arguments
break
;;
-*) # any other - argument
errMsg "--- UNKNOWN OPTION ---"
;;
*) # end of arguments
break
;;
esac
shift # next option
done
#
# get outfile
outfile="$1"
fi
# test that outfile provided
[ "$outfile" = "" ] && errMsg "NO OUTPUT FILE SPECIFIED"
# set up temporary file
tmp0="$dir/captcha_0_$$.miff"
trap "rm -f $tmp0;" 0
trap "rm -f $tmp0; exit 1" 1 2 3 15
trap "rm -f $tmp0; exit 1" ERR
# compute random positions and orientations and characters
xx1=`convert xc: -format "%[fx: -120 + sign(rand()-0.5)*floor(5*rand()+0.5)]" info:`
xx2=`convert xc: -format "%[fx: -72 + sign(rand()-0.5)*floor(5*rand()+0.5)]" info:`
xx3=`convert xc: -format "%[fx: -24 + sign(rand()-0.5)*floor(5*rand()+0.5)]" info:`
xx4=`convert xc: -format "%[fx: 24 + sign(rand()-0.5)*floor(5*rand()+0.5)]" info:`
xx5=`convert xc: -format "%[fx: 72 + sign(rand()-0.5)*floor(5*rand()+0.5)]" info:`
xx6=`convert xc: -format "%[fx: 120 + sign(rand()-0.5)*floor(5*rand()+0.5)]" info:`
yy1=`convert xc: -format "%[fx: sign(rand()-0.5)*floor(10*rand()+0.5)]" info:`
yy2=`convert xc: -format "%[fx: sign(rand()-0.5)*floor(10*rand()+0.5)]" info:`
yy3=`convert xc: -format "%[fx: sign(rand()-0.5)*floor(10*rand()+0.5)]" info:`
yy4=`convert xc: -format "%[fx: sign(rand()-0.5)*floor(10*rand()+0.5)]" info:`
yy5=`convert xc: -format "%[fx: sign(rand()-0.5)*floor(10*rand()+0.5)]" info:`
yy6=`convert xc: -format "%[fx: sign(rand()-0.5)*floor(10*rand()+0.5)]" info:`
rr1=`convert xc: -format "%[fx: sign(rand()-0.5)*floor($angle*rand()+0.5)]" info:`
rr2=`convert xc: -format "%[fx: sign(rand()-0.5)*floor($angle*rand()+0.5)]" info:`
rr3=`convert xc: -format "%[fx: sign(rand()-0.5)*floor($angle*rand()+0.5)]" info:`
rr4=`convert xc: -format "%[fx: sign(rand()-0.5)*floor($angle*rand()+0.5)]" info:`
rr5=`convert xc: -format "%[fx: sign(rand()-0.5)*floor($angle*rand()+0.5)]" info:`
rr6=`convert xc: -format "%[fx: sign(rand()-0.5)*floor($angle*rand()+0.5)]" info:`
ss1=`convert xc: -format "%[fx: sign(rand()-0.5)*floor($angle*rand()+0.5)]" info:`
ss2=`convert xc: -format "%[fx: sign(rand()-0.5)*floor($angle*rand()+0.5)]" info:`
ss3=`convert xc: -format "%[fx: sign(rand()-0.5)*floor($angle*rand()+0.5)]" info:`
ss4=`convert xc: -format "%[fx: sign(rand()-0.5)*floor($angle*rand()+0.5)]" info:`
ss5=`convert xc: -format "%[fx: sign(rand()-0.5)*floor($angle*rand()+0.5)]" info:`
ss6=`convert xc: -format "%[fx: sign(rand()-0.5)*floor($angle*rand()+0.5)]" info:`
if [ "$mode" = "both" ]; then
or1="rotate $rr1 skewX $ss1"
or2="rotate $rr2 skewX $ss2"
or3="rotate $rr3 skewX $ss3"
or4="rotate $rr4 skewX $ss4"
or5="rotate $rr5 skewX $ss5"
or6="rotate $rr6 skewX $ss6"
elif [ "$mode" = "skew" ]; then
or1="skewX $ss1"
or2="skewX $ss2"
or3="skewX $ss3"
or4="skewX $ss4"
or5="skewX $ss5"
or6="skewX $ss6"
elif [ "$mode" = "rotate" ]; then
or1="rotate $rr1"
or2="rotate $rr2"
or3="rotate $rr3"
or4="rotate $rr4"
or5="rotate $rr5"
or6="rotate $rr6"
fi
chars="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9"
charsArray=($chars)
nchars=${#charsArray[*]}
nchars1=`convert xc: -format "%[fx: $nchars - 1]" info:`
pp1=`convert xc: -format "%[fx: floor($nchars1*rand()+0.5)]" info:`
pp2=`convert xc: -format "%[fx: floor($nchars1*rand()+0.5)]" info:`
pp3=`convert xc: -format "%[fx: floor($nchars1*rand()+0.5)]" info:`
pp4=`convert xc: -format "%[fx: floor($nchars1*rand()+0.5)]" info:`
pp5=`convert xc: -format "%[fx: floor($nchars1*rand()+0.5)]" info:`
pp6=`convert xc: -format "%[fx: floor($nchars1*rand()+0.5)]" info:`
cc1=${charsArray[$pp1]}
cc2=${charsArray[$pp2]}
cc3=${charsArray[$pp3]}
cc4=${charsArray[$pp4]}
cc5=${charsArray[$pp5]}
cc6=${charsArray[$pp6]}
fx=1.1
fy=2
bx1=`convert xc: -format "%[fx: 150 + $fx*$xx1]" info:`
bx2=`convert xc: -format "%[fx: 150 + $fx*$xx2]" info:`
bx3=`convert xc: -format "%[fx: 150 + $fx*$xx3]" info:`
bx4=`convert xc: -format "%[fx: 150 + $fx*$xx4]" info:`
bx5=`convert xc: -format "%[fx: 150 + $fx*$xx5]" info:`
bx6=`convert xc: -format "%[fx: 150 + $fx*$xx6]" info:`
by1=`convert xc: -format "%[fx: 40 + $fy*$yy1]" info:`
by2=`convert xc: -format "%[fx: 40 + $fy*$yy2]" info:`
by3=`convert xc: -format "%[fx: 40 + $fy*$yy3]" info:`
by4=`convert xc: -format "%[fx: 40 + $fy*$yy4]" info:`
by5=`convert xc: -format "%[fx: 40 + $fy*$yy5]" info:`
by6=`convert xc: -format "%[fx: 40 + $fy*$yy6]" info:`
printf "$outfile,$cc1,$bx1,$by1\n" >> captcha_bash_annotations.csv
printf "$outfile,$cc2,$bx2,$by2\n" >> captcha_bash_annotations.csv
printf "$outfile,$cc3,$bx3,$by3\n" >> captcha_bash_annotations.csv
printf "$outfile,$cc4,$bx4,$by4\n" >> captcha_bash_annotations.csv
printf "$outfile,$cc5,$bx5,$by5\n" >> captcha_bash_annotations.csv
printf "$outfile,$cc6,$bx6,$by6\n" >> captcha_bash_annotations.csv
# create image
convert -size 290x70 xc:$undercolor -bordercolor $bordercolor -border 5 \
-fill black -font $font -pointsize $pointsize \
-draw "translate ${xx1},${yy1} $or1 gravity center text 0,0 '$cc1'" \
-draw "translate ${xx2},${yy2} $or2 gravity center text 0,0 '$cc2'" \
-draw "translate ${xx3},${yy3} $or3 gravity center text 0,0 '$cc3'" \
-draw "translate ${xx4},${yy4} $or4 gravity center text 0,0 '$cc4'" \
-draw "translate ${xx5},${yy5} $or5 gravity center text 0,0 '$cc5'" \
-draw "translate ${xx6},${yy6} $or6 gravity center text 0,0 '$cc6'" \
-fill none \
-draw "bezier ${bx1},${by1} ${bx2},${by2} ${bx3},${by3} ${bx4},${by4}" \
-draw "polyline ${bx4},${by4} ${bx5},${by5} ${bx6},${by6}" \
$tmp0
# resize if necessary
if [ "$resize" != "100" ]; then
convert $tmp0 -resize ${resize}% "$outfile"
else
convert $tmp0 "$outfile"
fi
exit 0