jcaptcha 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b25d10fe1d07d0200231b937be8137401bbcbeab
4
+ data.tar.gz: f1f9d06dc60a8f2ecf02ae4dc7ba83ef7c48f0cf
5
+ SHA512:
6
+ metadata.gz: 493421f2cb491d419e86c8f165cb5057b03ee78f685facb2a94cf55eeafbcb4339a655bbc2ba56d84f39fb6ed54db9dd7e8fa12cf6933a4b117f3823693ee3fd
7
+ data.tar.gz: 168035e4b53e2ffd337a22cf9ae312f5a71d6c53bfb68b1810f02fedbe7d8c69031359cfdc28dafb0baec63653df7a396e6b4485c1e2a9486bd6911342898839
@@ -0,0 +1,31 @@
1
+ # Jcaptcha
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'jcaptcha'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install jcaptcha
18
+
19
+ ## Usage
20
+
21
+ TODO: Jcaptcha.create("1234")
22
+
23
+ ## Contributing
24
+
25
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jcaptcha.
26
+
27
+
28
+ ## License
29
+
30
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
31
+
@@ -0,0 +1,69 @@
1
+ require "jcaptcha/version"
2
+ require 'open3'
3
+ require 'active_support/all'
4
+
5
+ module Jcaptcha
6
+
7
+ class << self
8
+ def random_color
9
+ color = [random_color_seed, random_color_seed, random_color_seed]
10
+ color[rand(3)] = 0.to_s(8)
11
+ color
12
+ end
13
+
14
+ def random_color_seed
15
+ (rand(150) + 10).to_s(8)
16
+ end
17
+
18
+ def rand_line_top(text_top, font_size)
19
+ text_top + rand(font_size * 0.7).to_i
20
+ end
21
+
22
+ # Create Captcha image by code
23
+ def create(code)
24
+ chars = code.split('')
25
+ all_left = 20
26
+ font_size = 45
27
+ full_height = font_size
28
+ full_width = font_size * chars.size
29
+ size = "#{full_width}x#{full_height}"
30
+ half_width = full_width / 2
31
+ text_top = 0
32
+ text_left = 0 - (font_size * 0.28).to_i
33
+ stroke_width = (font_size * 0.05).to_i + 1
34
+ text_width = font_size + text_left
35
+ text_opts = []
36
+ line_opts = []
37
+
38
+ chars.each_with_index do |char, i|
39
+ rgb = random_color
40
+ text_color = "rgba(#{rgb.join(',')}, 1)"
41
+ line_color = "rgba(#{rgb.join(',')}, 0.6)"
42
+ text_opts << %(-fill '#{text_color}' -draw 'text #{(text_left + text_width) * i + all_left},#{text_top} "#{char}"')
43
+ left_y = rand_line_top(text_top, font_size)
44
+ right_x = half_width + (half_width * 0.3).to_i
45
+ right_y = rand_line_top(text_top, font_size)
46
+ line_opts << %(-draw 'stroke #{line_color} line #{rand(10)},#{left_y} #{right_x},#{right_y}')
47
+ end
48
+
49
+ command = <<-CODE
50
+ convert -size #{size} \
51
+ -strokewidth #{stroke_width} \
52
+ #{line_opts.join(' ')} \
53
+ -pointsize #{font_size} -weight 500 \
54
+ #{text_opts.join(' ')} \
55
+ -wave #{rand(2) + 3}x#{rand(2) + 1} \
56
+ -rotate #{rand(10) - 5} \
57
+ -gravity NorthWest -sketch 1x10+#{rand(2)} \
58
+ -fill none \
59
+ -implode 0.3 -trim label:- png:-
60
+ CODE
61
+
62
+ command.strip!
63
+ out, err, _st = Open3.capture3(command)
64
+ raise "RuCaptcha: #{err.strip}" if err.present?
65
+ out
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,3 @@
1
+ module Jcaptcha
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jcaptcha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - moon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: create captcha for api based project
56
+ email:
57
+ - zhoubjnb@163.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - lib/jcaptcha.rb
64
+ - lib/jcaptcha/version.rb
65
+ homepage: https://github.com/MoonShining/jcaptcha
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.6.4
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: create captcha for api based project
89
+ test_files: []