rucaptcha 1.1.2 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f9a2ec29d415f469e44f42688627705fea9d9fb1
4
- data.tar.gz: 36a5267fa06eae4bfe53f7046a4e72db2127c610
3
+ metadata.gz: 7b416a66df71165fcd0c026e7aafbb858e1b3e39
4
+ data.tar.gz: e796e5f306d7b8a4f73bf46cf598257761caae9e
5
5
  SHA512:
6
- metadata.gz: 2d1d97a3e1bffe49105835cc42c17039c90b1d32843030c79f20becfbef9c81cef0c380634df1ccca5532e727f6bcb7a2069c831799830da5ef3fc63b9d8fd5c
7
- data.tar.gz: 552d3a13ff27c6a6f54ca9cbe1d4e9315ef1c469212eee210e8da7fb5d2e09f4f5fbe323ce3de593d37c9428401ad5bd01fcdc2bcd54476231acb6d3f24fa013
6
+ metadata.gz: 9c69ac42919fd8cdfe783f570efcbb682296e4394c1e61a9ffb211abf14796eed889d0fa0b6fbb8ee58c759af9036bb93add63c1e33c418267c4139a57b107d5
7
+ data.tar.gz: b60ee18e344f4ab2bc31ea6cef1e7cdbc5f69092ef93ccd860c1178e65624ed301e1e927f14ed6e0e07fd1b46f0ce4f78b6acc601558c0d772d5b8f493f6cc8e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ 1.1.4
2
+ -----
3
+
4
+ - Fix #35 just give a warning message if not setup a right cache_store, only raise on :null_store.
5
+
1
6
  1.1.2
2
7
  -----
3
8
 
@@ -3,6 +3,7 @@ require 'open3'
3
3
  module RuCaptcha
4
4
  class Captcha
5
5
  class << self
6
+ # Genrate ranom RGB color
6
7
  def random_color
7
8
  if RuCaptcha.config.style == :colorful
8
9
  color1 = rand(56) + 15
@@ -16,83 +17,103 @@ module RuCaptcha
16
17
  end
17
18
  end
18
19
 
20
+ # Genrate random Captcha code
19
21
  def random_chars
20
22
  chars = SecureRandom.hex(RuCaptcha.config.len / 2).downcase
21
23
  chars.gsub!(/[0ol1]/i, (rand(8) + 2).to_s)
22
24
  chars
23
25
  end
24
26
 
25
- def rand_line_top(text_top, font_size)
26
- text_top + rand(font_size * 0.7).to_i
27
- end
28
-
29
27
  # Create Captcha image by code
30
28
  def create(code)
31
29
  chars = code.split('')
32
- all_left = 20
33
- font_size = RuCaptcha.config.font_size
34
- full_height = font_size
35
- full_width = font_size * chars.size
30
+ full_width = RuCaptcha.config.font_size * chars.size
31
+ full_height = RuCaptcha.config.font_size
36
32
  size = "#{full_width}x#{full_height}"
33
+
34
+ return convert_for_windows(size, code) if Gem.win_platform?
35
+
36
+ opts = command_line_opts(chars, full_width)
37
+ convert(size, opts)
38
+ end
39
+
40
+ private
41
+
42
+ def command_line_opts(chars, full_width)
43
+ font_size = RuCaptcha.config.font_size
44
+ all_left = 20
37
45
  half_width = full_width / 2
38
46
  text_top = 0
39
47
  text_left = 0 - (font_size * 0.28).to_i
40
- stroke_width = (font_size * 0.05).to_i + 1
41
48
  text_width = font_size + text_left
42
- text_opts = []
43
- line_opts = []
44
49
 
45
- rgbs = []
46
- chars.count.times do |i|
47
- color = random_color
48
- if i > 0
49
- preview_color = rgbs[i - 1]
50
- # Avoid color same as preview color
51
- if color.index(color.min) == preview_color.index(preview_color.min) &&
52
- color.index(color.max) == preview_color.index(preview_color.max)
53
- # adjust RGB order
54
- color = [color[1], color[2], color[0]]
55
- end
56
- end
57
- rgbs << color
58
- end
50
+ opts = { text: [], line: [] }
51
+ rgbs = uniq_rgbs_for_each_chars(chars)
59
52
 
60
53
  chars.each_with_index do |char, i|
61
54
  rgb = RuCaptcha.config.style == :colorful ? rgbs[i] : rgbs[0]
62
55
  text_color = "rgba(#{rgb.join(',')}, 1)"
63
56
  line_color = "rgba(#{rgb.join(',')}, 0.6)"
64
- text_opts << %(-fill '#{text_color}' -draw 'text #{(text_left + text_width) * i + all_left},#{text_top} "#{char}"')
57
+ opts[:text] << %(-fill '#{text_color}' -draw 'text #{(text_left + text_width) * i + all_left},#{text_top} "#{char}"')
58
+
65
59
  left_y = rand_line_top(text_top, font_size)
66
60
  right_x = half_width + (half_width * 0.3).to_i
67
61
  right_y = rand_line_top(text_top, font_size)
68
- line_opts << %(-draw 'stroke #{line_color} line #{rand(10)},#{left_y} #{right_x},#{right_y}')
62
+ opts[:line] << %(-draw 'stroke #{line_color} line #{rand(10)},#{left_y} #{right_x},#{right_y}')
69
63
  end
64
+ opts
65
+ end
70
66
 
67
+ def convert(size, opts)
68
+ stroke_width = (RuCaptcha.config.font_size * 0.05).to_i + 1
71
69
  command = <<-CODE
72
70
  convert -size #{size} \
73
71
  -strokewidth #{stroke_width} \
74
- #{line_opts.join(' ')} \
75
- -pointsize #{font_size} -weight 500 \
76
- #{text_opts.join(' ')} \
72
+ #{opts[:line].join(' ')} \
73
+ -pointsize #{RuCaptcha.config.font_size} -weight 500 \
74
+ #{opts[:text].join(' ')} \
77
75
  -wave #{rand(2) + 3}x#{rand(2) + 1} \
78
76
  -rotate #{rand(10) - 5} \
79
77
  -gravity NorthWest -sketch 1x10+#{rand(2)} \
80
78
  -fill none \
81
79
  -implode #{RuCaptcha.config.implode} -trim label:- png:-
82
80
  CODE
81
+ command.strip!
82
+ out, err, _st = Open3.capture3(command)
83
+ warn " RuCaptcha #{err.strip}" if err.present?
84
+ out
85
+ end
83
86
 
84
- if Gem.win_platform?
85
- png_file_path = Rails.root.join('tmp', 'cache', "#{code}.png")
86
- command = "convert -size #{size} xc:White -gravity Center -weight 12 -pointsize 20 -annotate 0 \"#{code}\" -trim #{png_file_path}"
87
- out, err, _st = Open3.capture3(command)
88
- warn " RuCaptcha #{err.strip}" if err.present?
89
- png_file_path
90
- else
91
- command.strip!
92
- out, err, _st = Open3.capture3(command)
93
- warn " RuCaptcha #{err.strip}" if err.present?
94
- out
87
+ # Generate a simple captcha image for Windows Platform
88
+ def convert_for_windows(size, code)
89
+ png_file_path = Rails.root.join('tmp', 'cache', "#{code}.png")
90
+ command = "convert -size #{size} xc:White -gravity Center -weight 12 -pointsize 20 -annotate 0 \"#{code}\" -trim #{png_file_path}"
91
+ _out, err, _st = Open3.capture3(command)
92
+ warn " RuCaptcha #{err.strip}" if err.present?
93
+ png_file_path
94
+ end
95
+
96
+ # Geneate a uniq rgba colors for each chars
97
+ def uniq_rgbs_for_each_chars(chars)
98
+ rgbs = []
99
+ chars.count.times do |i|
100
+ color = random_color
101
+ if i > 0
102
+ preview_color = rgbs[i - 1]
103
+ # Avoid color same as preview color
104
+ if color.index(color.min) == preview_color.index(preview_color.min) &&
105
+ color.index(color.max) == preview_color.index(preview_color.max)
106
+ # adjust RGB order
107
+ color = [color[1], color[2], color[0]]
108
+ end
109
+ end
110
+ rgbs << color
95
111
  end
112
+ rgbs
113
+ end
114
+
115
+ def rand_line_top(text_top, font_size)
116
+ text_top + rand(font_size * 0.7).to_i
96
117
  end
97
118
 
98
119
  def warn(msg)
@@ -11,16 +11,21 @@ module RuCaptcha
11
11
  cache_store = RuCaptcha.config.cache_store
12
12
  store_name = cache_store.is_a?(Array) ? cache_store.first : cache_store
13
13
  if [:memory_store, :null_store, :file_store].include?(store_name)
14
- raise "
14
+ msg = "
15
15
 
16
16
  RuCaptcha's cache_store requirements are stored across processes and machines,
17
17
  such as :mem_cache_store, :redis_store, or other distributed storage.
18
18
  But your current set is :#{store_name}.
19
19
 
20
20
  Please make config file `config/initializes/rucaptcha.rb` to setup `cache_store`.
21
- More infomation please read GitHub rucaptcha README file.
21
+ More infomation please read GitHub RuCaptcha README file.
22
22
 
23
23
  "
24
+ if store_name == :null_store
25
+ raise msg
26
+ else
27
+ puts msg
28
+ end
24
29
  end
25
30
  end
26
31
  end
@@ -1,3 +1,3 @@
1
1
  module RuCaptcha
2
- VERSION = '1.1.2'
2
+ VERSION = '1.1.4'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rucaptcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-08 00:00:00.000000000 Z
11
+ date: 2016-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties