From 682aa39ec0330c0aa2707a351bad8fc25356c9a0 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 6 Sep 2018 19:33:18 +0800 Subject: [PATCH] Add for use outline style, and reduce colors down to 5 (red, blue, green, yellow and black). --- CHANGELOG.md | 6 + Makefile | 5 + README.md | 13 +- Rakefile | 3 +- ext/rucaptcha/colors.h | 421 ++++++++------------------------- ext/rucaptcha/rucaptcha.c | 30 ++- lib/rucaptcha.rb | 4 +- lib/rucaptcha/configuration.rb | 2 + lib/rucaptcha/version.rb | 2 +- test | 2 - 10 files changed, 144 insertions(+), 344 deletions(-) create mode 100644 Makefile delete mode 100755 test diff --git a/CHANGELOG.md b/CHANGELOG.md index 1dca5f7..7aa2118 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +2.3.0 +------- + +- Add `config.outline` for use outline style. +- Reduce colors down to 5 (red, blue, green, yellow and black). + 2.2.0 ----- diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..105eaaa --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +compile: + rake clean + rake compile +test: + rake preview > ~/Desktop/rucaptcha-test.gif && open ~/Desktop/rucaptcha-test.gif diff --git a/README.md b/README.md index 897b43a..67e75a5 100644 --- a/README.md +++ b/README.md @@ -2,15 +2,20 @@ [![Gem Version](https://badge.fury.io/rb/rucaptcha.svg)](https://badge.fury.io/rb/rucaptcha) [![Build Status](https://travis-ci.org/huacnlee/rucaptcha.svg)](https://travis-ci.org/huacnlee/rucaptcha) -[![Code Climate](https://codeclimate.com/github/huacnlee/rucaptcha/badges/gpa.svg)](https://codeclimate.com/github/huacnlee/rucaptcha) This is a Captcha gem for Rails Applications which generates captcha image by C code. +[中文介绍和使用说明](https://ruby-china.org/topics/27832) + ## Example - +**default style** -[中文介绍和使用说明](https://ruby-china.org/topics/27832) +![rucaptcha-0](https://user-images.githubusercontent.com/5518/45154624-c471a780-b20a-11e8-8bec-6c133838d53a.gif) ![rucaptcha-1](https://user-images.githubusercontent.com/5518/45154625-c471a780-b20a-11e8-8d09-dfb6ace6e9ba.gif) ![rucaptcha-2](https://user-images.githubusercontent.com/5518/45154628-c50a3e00-b20a-11e8-9afd-7c200b663dbc.gif) ![rucaptcha-3](https://user-images.githubusercontent.com/5518/45154629-c50a3e00-b20a-11e8-88d1-536717664e88.gif) ![rucaptcha-4](https://user-images.githubusercontent.com/5518/45154630-c50a3e00-b20a-11e8-9aa8-9b896a177e84.gif) + +**outline enable** + +![rucaptcha-b0](https://user-images.githubusercontent.com/5518/45154631-c5a2d480-b20a-11e8-9b63-febdaa5e5c9c.gif) ![rucaptcha-b1](https://user-images.githubusercontent.com/5518/45154632-c5a2d480-b20a-11e8-896b-0fe17bc0b5c5.gif) ![rucaptcha-b2](https://user-images.githubusercontent.com/5518/45154633-c63b6b00-b20a-11e8-8226-7d516764634c.gif) ![rucaptcha-b3](https://user-images.githubusercontent.com/5518/45154634-c63b6b00-b20a-11e8-9781-c22a3109616b.gif) ![rucaptcha-b4](https://user-images.githubusercontent.com/5518/45154635-c63b6b00-b20a-11e8-8e12-52a30e686339.gif) ## Feature @@ -46,6 +51,8 @@ RuCaptcha.configure do # self.length = 5 # enable/disable Strikethrough. # self.strikethrough = true + # enable/disable Outline style, for hard mode + # self.outline = false end ``` diff --git a/Rakefile b/Rakefile index ed51560..dba65f8 100644 --- a/Rakefile +++ b/Rakefile @@ -13,6 +13,7 @@ task default: :spec task :preview do require 'rucaptcha' - res = RuCaptcha.create(1, 3, 1) + res = RuCaptcha.create(1, 5, 1, 0) + $stderr.puts res[0] puts res[1] end diff --git a/ext/rucaptcha/colors.h b/ext/rucaptcha/colors.h index 876ac31..840bf98 100644 --- a/ext/rucaptcha/colors.h +++ b/ext/rucaptcha/colors.h @@ -1,345 +1,118 @@ // Colors from // https://material.io/guidelines/style/color.html#color-ui-color-palette +// Color covert HEX RGB to \x??\x??\x?? static char *colors[] = { // Black 500 #000000 "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\x11\x11\x11" - "\x11\x11\x11" - "\x11\x11\x11" - "\x11\x11\x11" - "\x11\x11\x11" - "\x11\x11\x11" - "\x11\x11\x11" - "\x11\x11\x11" - "\x11\x11\x11" - "\x11\x11\x11" - "\x11\x11\x11" - "\x11\x11\x11" - "\x11\x11\x11" - "\x11\x11\x11" - "\x11\x11\x11" + "\x00\x00\x00" + "\x00\x00\x00" + "\x00\x00\x00" + "\x00\x00\x00" + "\x00\x00\x00" + "\x00\x00\x00" + "\x00\x00\x00" + "\x00\x00\x00" + "\x00\x00\x00" + "\x00\x00\x00" + "\x00\x00\x00" + "\x00\x00\x00" + "\x00\x00\x00" + "\x00\x00\x00" + "\x00\x00\x00" "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - // Red 500 #F44336 + // Red A700 #D50000 "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\xF4\x43\x36" - "\xF4\x43\x36" - "\xF4\x43\x36" - "\xF4\x43\x36" - "\xF4\x43\x36" - "\xF4\x43\x36" - "\xF4\x43\x36" - "\xF4\x43\x36" - "\xF4\x43\x36" - "\xF4\x43\x36" - "\xF4\x43\x36" - "\xF4\x43\x36" - "\xF4\x43\x36" - "\xF4\x43\x36" - "\xF4\x43\x36" + "\xD5\x00\x00" + "\xD5\x00\x00" + "\xD5\x00\x00" + "\xD5\x00\x00" + "\xD5\x00\x00" + "\xD5\x00\x00" + "\xD5\x00\x00" + "\xD5\x00\x00" + "\xD5\x00\x00" + "\xD5\x00\x00" + "\xD5\x00\x00" + "\xD5\x00\x00" + "\xD5\x00\x00" + "\xD5\x00\x00" + "\xD5\x00\x00" "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - // Pink 500 #E91E63 + // Deep Orange A700 #DD2C00 "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\xE9\x1E\x63" - "\xE9\x1E\x63" - "\xE9\x1E\x63" - "\xE9\x1E\x63" - "\xE9\x1E\x63" - "\xE9\x1E\x63" - "\xE9\x1E\x63" - "\xE9\x1E\x63" - "\xE9\x1E\x63" - "\xE9\x1E\x63" - "\xE9\x1E\x63" - "\xE9\x1E\x63" - "\xE9\x1E\x63" - "\xE9\x1E\x63" - "\xE9\x1E\x63" + "\xDD\x2C\x00" + "\xDD\x2C\x00" + "\xDD\x2C\x00" + "\xDD\x2C\x00" + "\xDD\x2C\x00" + "\xDD\x2C\x00" + "\xDD\x2C\x00" + "\xDD\x2C\x00" + "\xDD\x2C\x00" + "\xDD\x2C\x00" + "\xDD\x2C\x00" + "\xDD\x2C\x00" + "\xDD\x2C\x00" + "\xDD\x2C\x00" + "\xDD\x2C\x00" "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - // Purple 500 #9C27B0 + // Blue A700 #2962FF "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\x9C\x27\xB0" - "\x9C\x27\xB0" - "\x9C\x27\xB0" - "\x9C\x27\xB0" - "\x9C\x27\xB0" - "\x9C\x27\xB0" - "\x9C\x27\xB0" - "\x9C\x27\xB0" - "\x9C\x27\xB0" - "\x9C\x27\xB0" - "\x9C\x27\xB0" - "\x9C\x27\xB0" - "\x9C\x27\xB0" - "\x9C\x27\xB0" - "\x9C\x27\xB0" + "\x29\x62\xFF" + "\x29\x62\xFF" + "\x29\x62\xFF" + "\x29\x62\xFF" + "\x29\x62\xFF" + "\x29\x62\xFF" + "\x29\x62\xFF" + "\x29\x62\xFF" + "\x29\x62\xFF" + "\x29\x62\xFF" + "\x29\x62\xFF" + "\x29\x62\xFF" + "\x29\x62\xFF" + "\x29\x62\xFF" + "\x29\x62\xFF" "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - // Deep Purple 500 #673AB7 + // Green A700 #00C853 "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\x67\x3A\xB7" - "\x67\x3A\xB7" - "\x67\x3A\xB7" - "\x67\x3A\xB7" - "\x67\x3A\xB7" - "\x67\x3A\xB7" - "\x67\x3A\xB7" - "\x67\x3A\xB7" - "\x67\x3A\xB7" - "\x67\x3A\xB7" - "\x67\x3A\xB7" - "\x67\x3A\xB7" - "\x67\x3A\xB7" - "\x67\x3A\xB7" - "\x67\x3A\xB7" + "\x00\xC8\x53" + "\x00\xC8\x53" + "\x00\xC8\x53" + "\x00\xC8\x53" + "\x00\xC8\x53" + "\x00\xC8\x53" + "\x00\xC8\x53" + "\x00\xC8\x53" + "\x00\xC8\x53" + "\x00\xC8\x53" + "\x00\xC8\x53" + "\x00\xC8\x53" + "\x00\xC8\x53" + "\x00\xC8\x53" + "\x00\xC8\x53" "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - // Indigo 500 #3F51B5 + // Yellow A700 #FFD600 "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\x3F\x51\xB5" - "\x3F\x51\xB5" - "\x3F\x51\xB5" - "\x3F\x51\xB5" - "\x3F\x51\xB5" - "\x3F\x51\xB5" - "\x3F\x51\xB5" - "\x3F\x51\xB5" - "\x3F\x51\xB5" - "\x3F\x51\xB5" - "\x3F\x51\xB5" - "\x3F\x51\xB5" - "\x3F\x51\xB5" - "\x3F\x51\xB5" - "\x3F\x51\xB5" - "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - - // Blue 500 #2196F3 - "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\x21\x96\xF3" - "\x21\x96\xF3" - "\x21\x96\xF3" - "\x21\x96\xF3" - "\x21\x96\xF3" - "\x21\x96\xF3" - "\x21\x96\xF3" - "\x21\x96\xF3" - "\x21\x96\xF3" - "\x21\x96\xF3" - "\x21\x96\xF3" - "\x21\x96\xF3" - "\x21\x96\xF3" - "\x21\x96\xF3" - "\x21\x96\xF3" - "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - - // Light Blue 500 #03A9F4 - "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\x03\xA9\xF4" - "\x03\xA9\xF4" - "\x03\xA9\xF4" - "\x03\xA9\xF4" - "\x03\xA9\xF4" - "\x03\xA9\xF4" - "\x03\xA9\xF4" - "\x03\xA9\xF4" - "\x03\xA9\xF4" - "\x03\xA9\xF4" - "\x03\xA9\xF4" - "\x03\xA9\xF4" - "\x03\xA9\xF4" - "\x03\xA9\xF4" - "\x03\xA9\xF4" - "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - - // Cyan 500 #00BCD4 - "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\x00\xBC\xD4" - "\x00\xBC\xD4" - "\x00\xBC\xD4" - "\x00\xBC\xD4" - "\x00\xBC\xD4" - "\x00\xBC\xD4" - "\x00\xBC\xD4" - "\x00\xBC\xD4" - "\x00\xBC\xD4" - "\x00\xBC\xD4" - "\x00\xBC\xD4" - "\x00\xBC\xD4" - "\x00\xBC\xD4" - "\x00\xBC\xD4" - "\x00\xBC\xD4" - "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - - // Teal 500 #009688 - "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\x00\x96\x88" - "\x00\x96\x88" - "\x00\x96\x88" - "\x00\x96\x88" - "\x00\x96\x88" - "\x00\x96\x88" - "\x00\x96\x88" - "\x00\x96\x88" - "\x00\x96\x88" - "\x00\x96\x88" - "\x00\x96\x88" - "\x00\x96\x88" - "\x00\x96\x88" - "\x00\x96\x88" - "\x00\x96\x88" - "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - - // Green 500 #4CAF50 - "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\x4C\xAF\x50" - "\x4C\xAF\x50" - "\x4C\xAF\x50" - "\x4C\xAF\x50" - "\x4C\xAF\x50" - "\x4C\xAF\x50" - "\x4C\xAF\x50" - "\x4C\xAF\x50" - "\x4C\xAF\x50" - "\x4C\xAF\x50" - "\x4C\xAF\x50" - "\x4C\xAF\x50" - "\x4C\xAF\x50" - "\x4C\xAF\x50" - "\x4C\xAF\x50" - "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - - // Light Green 500 #8BC34A - "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\x8B\xC3\x4A" - "\x8B\xC3\x4A" - "\x8B\xC3\x4A" - "\x8B\xC3\x4A" - "\x8B\xC3\x4A" - "\x8B\xC3\x4A" - "\x8B\xC3\x4A" - "\x8B\xC3\x4A" - "\x8B\xC3\x4A" - "\x8B\xC3\x4A" - "\x8B\xC3\x4A" - "\x8B\xC3\x4A" - "\x8B\xC3\x4A" - "\x8B\xC3\x4A" - "\x8B\xC3\x4A" - "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - - // Lime 500 #CDDC39 - "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\xCD\xDC\x39" - "\xCD\xDC\x39" - "\xCD\xDC\x39" - "\xCD\xDC\x39" - "\xCD\xDC\x39" - "\xCD\xDC\x39" - "\xCD\xDC\x39" - "\xCD\xDC\x39" - "\xCD\xDC\x39" - "\xCD\xDC\x39" - "\xCD\xDC\x39" - "\xCD\xDC\x39" - "\xCD\xDC\x39" - "\xCD\xDC\x39" - "\xCD\xDC\x39" - "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - - // Yellow 500 #FFEB3B - "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\xFF\xEB\x3B" - "\xFF\xEB\x3B" - "\xFF\xEB\x3B" - "\xFF\xEB\x3B" - "\xFF\xEB\x3B" - "\xFF\xEB\x3B" - "\xFF\xEB\x3B" - "\xFF\xEB\x3B" - "\xFF\xEB\x3B" - "\xFF\xEB\x3B" - "\xFF\xEB\x3B" - "\xFF\xEB\x3B" - "\xFF\xEB\x3B" - "\xFF\xEB\x3B" - "\xFF\xEB\x3B" - "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - - // Amber 500 #FFC107 - "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\xFF\xC1\x07" - "\xFF\xC1\x07" - "\xFF\xC1\x07" - "\xFF\xC1\x07" - "\xFF\xC1\x07" - "\xFF\xC1\x07" - "\xFF\xC1\x07" - "\xFF\xC1\x07" - "\xFF\xC1\x07" - "\xFF\xC1\x07" - "\xFF\xC1\x07" - "\xFF\xC1\x07" - "\xFF\xC1\x07" - "\xFF\xC1\x07" - "\xFF\xC1\x07" - "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - - // Orange 500 #FF9800 - "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\xFF\x98\x00" - "\xFF\x98\x00" - "\xFF\x98\x00" - "\xFF\x98\x00" - "\xFF\x98\x00" - "\xFF\x98\x00" - "\xFF\x98\x00" - "\xFF\x98\x00" - "\xFF\x98\x00" - "\xFF\x98\x00" - "\xFF\x98\x00" - "\xFF\x98\x00" - "\xFF\x98\x00" - "\xFF\x98\x00" - "\xFF\x98\x00" - "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - - // Deep Orange 500 #FF5722 - "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\xFF\x57\x22" - "\xFF\x57\x22" - "\xFF\x57\x22" - "\xFF\x57\x22" - "\xFF\x57\x22" - "\xFF\x57\x22" - "\xFF\x57\x22" - "\xFF\x57\x22" - "\xFF\x57\x22" - "\xFF\x57\x22" - "\xFF\x57\x22" - "\xFF\x57\x22" - "\xFF\x57\x22" - "\xFF\x57\x22" - "\xFF\x57\x22" - "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04", - - // Brown 500 #795548 - "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0" - "\x79\x55\x48" - "\x79\x55\x48" - "\x79\x55\x48" - "\x79\x55\x48" - "\x79\x55\x48" - "\x79\x55\x48" - "\x79\x55\x48" - "\x79\x55\x48" - "\x79\x55\x48" - "\x79\x55\x48" - "\x79\x55\x48" - "\x79\x55\x48" - "\x79\x55\x48" - "\x79\x55\x48" - "\x79\x55\x48" + "\xFF\xD6\x00" + "\xFF\xD6\x00" + "\xFF\xD6\x00" + "\xFF\xD6\x00" + "\xFF\xD6\x00" + "\xFF\xD6\x00" + "\xFF\xD6\x00" + "\xFF\xD6\x00" + "\xFF\xD6\x00" + "\xFF\xD6\x00" + "\xFF\xD6\x00" + "\xFF\xD6\x00" + "\xFF\xD6\x00" + "\xFF\xD6\x00" + "\xFF\xD6\x00" "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04" -}; \ No newline at end of file +}; diff --git a/ext/rucaptcha/rucaptcha.c b/ext/rucaptcha/rucaptcha.c index a3367bc..402985c 100644 --- a/ext/rucaptcha/rucaptcha.c +++ b/ext/rucaptcha/rucaptcha.c @@ -1,6 +1,6 @@ // http://github.com/ITikhonov/captcha const int gifsize; -void captcha(unsigned char im[70*200], unsigned char l[8], int length, int i_line); +void captcha(unsigned char im[70*200], unsigned char l[8], int length, int i_line, int i_filter); void makegif(unsigned char im[70*200], unsigned char gif[gifsize], int style); #include @@ -85,10 +85,10 @@ static int letter(int n, int pos, unsigned char im[70*200], unsigned char swr[20 if((x-im)<70*200) *x=(*p)<<4; i++; } - return mpos; + return mpos + 3; } -#define NDOTS 100 +#define NDOTS 10 uint32_t dr[NDOTS]; @@ -97,7 +97,7 @@ static void line(unsigned char im[70*200], unsigned char swr[200], uint8_t s1) { int sk1=s1; for(x=0;x<199;x++) { if(sk1>=200) sk1=sk1%200; - int skew=sw[sk1]/16; + int skew=sw[sk1]/20; sk1+=swr[x]&0x3+1; unsigned char *i= im+(200*(45+skew)+x); i[0]=0; i[1]=0; i[200]=0; i[201]=0; @@ -118,6 +118,7 @@ static void dots(unsigned char im[70*200]) { i[202]=0xff; } } + static void blur(unsigned char im[70*200]) { unsigned char *i=im; int x,y; @@ -150,9 +151,9 @@ static void filter(unsigned char im[70*200]) { memmove(im,om,sizeof(om)); } -static const char *letters="abcdafahijklmnopqrstuvwxyz"; +static const char *letters="abcdyfhhijklmnopqrstuvwxyz"; -void captcha(unsigned char im[70*200], unsigned char l[8], int length, int i_line) { +void captcha(unsigned char im[70*200], unsigned char l[8], int length, int i_line, int i_filter) { unsigned char swr[200]; uint8_t s1,s2; @@ -173,11 +174,15 @@ void captcha(unsigned char im[70*200], unsigned char l[8], int length, int i_lin for(x=0;x /dev/null 2& > /dev/null -rake preview > /tmp/rucaptcha-test.gif && open /tmp/rucaptcha-test.gif \ No newline at end of file