diff options
author | Max Christian Pohle | 2015-10-12 04:15:25 +0200 |
---|---|---|
committer | Max Christian Pohle | 2015-10-12 04:15:25 +0200 |
commit | a66a946b86ff701e30b796c73367c791d21f9209 (patch) | |
tree | cf75cba357d36397bcc823bb4304bdea9b9ce805 /captcha.c | |
download | libcaptcha-a66a946b86ff701e30b796c73367c791d21f9209.tar.bz2 libcaptcha-a66a946b86ff701e30b796c73367c791d21f9209.zip |
version depends on gd and creates a traditional captcha
Diffstat (limited to 'captcha.c')
-rw-r--r-- | captcha.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/captcha.c b/captcha.c new file mode 100644 index 0000000..18f7d99 --- /dev/null +++ b/captcha.c | |||
@@ -0,0 +1,51 @@ | |||
1 | #include "captcha.h" | ||
2 | |||
3 | int captcha_print(char* text) | ||
4 | { | ||
5 | File* file = file_open("code.png"); | ||
6 | |||
7 | gdImagePtr im = gdImageCreateFromPngPtr(file->size, file->data); | ||
8 | gdImageColorTransparent(im, gdImageColorAllocate(im, 255, 255, 255)); | ||
9 | |||
10 | char text_width = strlen(text); | ||
11 | float letter_width = ((float) gdImageSX(im) / 26.00); | ||
12 | |||
13 | int image_width = letter_width * text_width; | ||
14 | int image_height = gdImageSY(im); | ||
15 | |||
16 | gdImagePtr image = gdImageCreateTrueColor(image_width, image_height); | ||
17 | gdImageColorTransparent(image, gdImageColorAllocate(image, 0, 0, 0)); | ||
18 | |||
19 | srand(time(NULL) * (*text)); | ||
20 | int i; | ||
21 | for(i=0; i<=10; i++) | ||
22 | { | ||
23 | int x1 = ((double) rand() / RAND_MAX) * image_width; | ||
24 | int y1 = ((double) rand() / RAND_MAX) * image_height; | ||
25 | gdImageFilledArc(image, x1, y1, x1 + 10, y1 + 10, ((double) rand() / RAND_MAX) * 360, ((double) rand() / RAND_MAX) * 360, gdImageColorAllocate(image, 100 + (rand() * 155), 150 + (rand() * 105), 150 + (rand() * 105)), gdEdged); | ||
26 | } | ||
27 | |||
28 | int pos; | ||
29 | for(pos=0; text[pos]!='\0'; pos++) | ||
30 | { | ||
31 | //printf("[pos:%d] %d\n", pos, (text[pos] - 65) ); | ||
32 | gdImageCopyMerge(image, im, | ||
33 | letter_width * pos, 0, | ||
34 | ((text[pos] - 65) * letter_width), 0, | ||
35 | letter_width, gdImageSY(image), 50); | ||
36 | } | ||
37 | |||
38 | puts("content-type: image/jpeg\n"); | ||
39 | gdImageJpeg(image, stdout, 80); | ||
40 | |||
41 | gdImageDestroy(image); | ||
42 | gdImageDestroy(im); | ||
43 | |||
44 | file_close(file); | ||
45 | return EXIT_SUCCESS; | ||
46 | } | ||
47 | |||
48 | int main() | ||
49 | { | ||
50 | return captcha_print("HALLELUJA"); | ||
51 | } | ||