struggle 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/struggle/fileupload.rb +115 -0
- data/lib/struggle/func.rb +104 -0
- data/lib/struggle/pager.rb +67 -0
- data/lib/struggle/pay_gateway.rb +49 -0
- data/lib/struggle/sms.rb +12 -0
- data/lib/struggle/tmagick.rb +52 -0
- data/lib/struggle/vcode.rb +48 -0
- data/lib/struggle.rb +14 -0
- data/struggle.gemspec +16 -0
- metadata +17 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9bbf88fe4cb0e679ed14abd2ccc9fcc9688231d1
|
4
|
+
data.tar.gz: 49e005b94ebffaed197071833cf52d85fe76572e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f147766b13ea1c80957937643bff582806a43b9b05b3544a54971e3a8c2f4756262a4726ec0143241aad87d444a04caf21554d64ba7bc3e4650b5417c6582b6e
|
7
|
+
data.tar.gz: b573ae89712ea8c06f6cf1e2a6f2636c13bd416397efde16b7dfe93c837028a346093395c29246fc2f8e631ccc7a64c929ea885863d9bddccb3e5690c7839f4e
|
@@ -0,0 +1,115 @@
|
|
1
|
+
module Struggle
|
2
|
+
class Fileupload
|
3
|
+
#file upload
|
4
|
+
#params: file->file stream,filepath->file save path,rule->can upload file format("jpg|xls"),minsize and maxsize->minsize<file's size<maxsize
|
5
|
+
#return: {state: true, result: "new filename"} or {state: false, result: "error message"}
|
6
|
+
def Fileupload.upload(file, filepath="", rule="jpg|xls", minsize=1, maxsize=4000)
|
7
|
+
result = Fileupload.rule_validata(file, rule, minsize, maxsize)
|
8
|
+
if result[:state]
|
9
|
+
sname = Fileupload.getname(file, filepath)
|
10
|
+
begin
|
11
|
+
unless Dir::exist?(filepath)
|
12
|
+
unless system("mkdir -p #{filepath}")
|
13
|
+
return {state: false, result: "目录创建失败,请于管理员联系"}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
File.open(filepath+sname, "wb") do |f|
|
17
|
+
f.write(file.read)
|
18
|
+
end
|
19
|
+
return {state: true, result: sname}
|
20
|
+
rescue
|
21
|
+
return {state: false, result: "写入文件失败:#{$!}"}
|
22
|
+
end
|
23
|
+
else
|
24
|
+
return {state: false, result: result[:message]}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
#validate file
|
29
|
+
#params:file->file stream,rule->can upload file format,minsize<file's size<maxsize
|
30
|
+
#return:{state: true} or return {state: false, message: "error message"}
|
31
|
+
def Fileupload.rule_validata(file, rule, minsize, maxsize)
|
32
|
+
rule_for = Fileupload.filename_validata(file, rule)
|
33
|
+
unless rule_for
|
34
|
+
return {state: false, message: "错误:文件格式不对,只允许上传#{rule}格式!\\n"}
|
35
|
+
end
|
36
|
+
if file.size<minsize*1024 || file.size>maxsize*1024
|
37
|
+
return {state: false, message: "错误:文件大小错误,只允许#{minsize}kb~#{maxsize}kb!\\n"}
|
38
|
+
end
|
39
|
+
return {state: true}
|
40
|
+
end
|
41
|
+
|
42
|
+
#validate file format
|
43
|
+
#params: file-> file stream; rule-> validate rule ("jpg|xls")
|
44
|
+
#return: validate result -> true | false
|
45
|
+
def Fileupload.filename_validata(file, rule)
|
46
|
+
!eval("/\.(#{rule})+$/").match(file.original_filename).blank?
|
47
|
+
end
|
48
|
+
|
49
|
+
#get new file's name
|
50
|
+
#params:filestream->file stream file name,filepath->file save path
|
51
|
+
#return:new file's name
|
52
|
+
def Fileupload.getname(filestream, filepath)
|
53
|
+
file_for = Fileupload.file_format(filestream.original_filename)
|
54
|
+
filename = Time.now.strftime("%Y%m%d%h%m%s")<<rand(99999).to_s<<file_for
|
55
|
+
file = filepath+filename
|
56
|
+
while File.exist?(file) do
|
57
|
+
filename = Time.now.strftime("%Y%m%d%h%m%s")<<rand(99999).to_s<<file_for
|
58
|
+
file = filepath+filename
|
59
|
+
end
|
60
|
+
filename
|
61
|
+
end
|
62
|
+
|
63
|
+
#get file format
|
64
|
+
#params: filename->file's name
|
65
|
+
#return: file format '.jpg','.exe'
|
66
|
+
def Fileupload.file_format(filename)
|
67
|
+
/\.[^\.]+$/.match(filename)[0]
|
68
|
+
end
|
69
|
+
|
70
|
+
#image upload
|
71
|
+
#params: file->file stream; filepath->file save path; rule->can upload file format("jpg|xls"); minsize and maxsize->minsize<file's size<maxsize; w->new image width, h->new image height
|
72
|
+
#return: {state: true, result: "new filename"} or {state: false, result: "error message"}
|
73
|
+
def Fileupload.imageupload(imgfile, filepath="", rule="jpg|jpeg", minsize=0, maxsize=2000, w=0, h=0)
|
74
|
+
result = Fileupload.rule_validata(imgfile, rule, minsize, maxsize)
|
75
|
+
if result[:state]
|
76
|
+
sname = Fileupload.getname(imgfile, filepath)
|
77
|
+
begin
|
78
|
+
unless Dir::exist?(filepath)
|
79
|
+
unless system("mkdir -p #{filepath}")
|
80
|
+
return {state: false, result: "目录创建失败,请于管理员联系"}
|
81
|
+
end
|
82
|
+
end
|
83
|
+
File.open(filepath+sname, "wb") do |f|
|
84
|
+
f.write(imgfile.read)
|
85
|
+
end
|
86
|
+
Fileupload.resize(filepath + sname, w, h)
|
87
|
+
return {state: true, result: sname}
|
88
|
+
rescue
|
89
|
+
return {state: false, result: "写入图片失败:#{$!}"}
|
90
|
+
end
|
91
|
+
else
|
92
|
+
return {state: false, result: result[:message]}
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
#delete dir
|
97
|
+
#params: dir-> dir path
|
98
|
+
#return true or false
|
99
|
+
def Fileupload.rmdir(dir)
|
100
|
+
system("rm -rf #{dir}")
|
101
|
+
end
|
102
|
+
|
103
|
+
#image resize
|
104
|
+
#params: imagepath-> image file path; w-> new image width; h-> new image height
|
105
|
+
def Fileupload.resize(imagepath, w, h)
|
106
|
+
img = Magick::Image.read(imagepath)[0]
|
107
|
+
if w==0 || h==0
|
108
|
+
w=img.columns
|
109
|
+
h=img.rows
|
110
|
+
end
|
111
|
+
newimg = img.resize_to_fill(w, h)
|
112
|
+
newimg.write(imagepath)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
module Struggle
|
2
|
+
class Func
|
3
|
+
# 判断是否含有中文
|
4
|
+
def self.ischinese(temp)
|
5
|
+
(temp=~/[\u4e00-\u9fa5]/).nil? ? false : true
|
6
|
+
end
|
7
|
+
|
8
|
+
# 判断是否只含有中英文,数字和下划线
|
9
|
+
def self.iscn_zn_num(temp)
|
10
|
+
(temp=~/^[\u4e00-\u9fa5_a-zA-Z0-9]+$/).nil? ? false : true
|
11
|
+
end
|
12
|
+
|
13
|
+
# 格式化时间
|
14
|
+
def self.formattime(time)
|
15
|
+
time.strftime("%Y-%m-%d %H:%M:%S")
|
16
|
+
end
|
17
|
+
|
18
|
+
# 限制文字长度
|
19
|
+
def self.truncate_u(text, length = 30, truncate_string = "...")
|
20
|
+
l=0
|
21
|
+
char_array=text.unpack("U*")
|
22
|
+
char_array.each_with_index do |c, i|
|
23
|
+
l = l+ (c<127 ? 0.5 : 1)
|
24
|
+
if l>=length
|
25
|
+
return char_array[0..i].pack("U*")+(i<char_array.length-1 ? truncate_string : "")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
return text
|
29
|
+
end
|
30
|
+
|
31
|
+
# 金额大写小转换
|
32
|
+
def self.uppercase(nums)
|
33
|
+
cstr = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]
|
34
|
+
cn_nums1 = ["元", "拾", "佰", "仟", "萬", "拾", "佰", "仟", "億", "拾", "佰", "仟"]
|
35
|
+
cn_nums2 = ['分', '角']
|
36
|
+
s = ""
|
37
|
+
# 整数部分
|
38
|
+
array = nums.to_s.split(".")
|
39
|
+
p h = array[0].to_s.split(//)
|
40
|
+
ai = h.count
|
41
|
+
h.each_with_index do |a, j|
|
42
|
+
s+=cstr[a.to_i]+cn_nums1[ai-1]
|
43
|
+
ai=ai-1
|
44
|
+
end
|
45
|
+
# 小数部分
|
46
|
+
p h1 = array[1].to_s.split(//)
|
47
|
+
aj = h1.count
|
48
|
+
h1.each_with_index do |o, p|
|
49
|
+
s+=cstr[o.to_i]+cn_nums2[aj-1]
|
50
|
+
aj=aj-1
|
51
|
+
end
|
52
|
+
rstr = ""
|
53
|
+
rstr=s.gsub("拾零", "拾")
|
54
|
+
rstr=rstr.gsub("零拾", "零");
|
55
|
+
rstr=rstr.gsub("零佰", "零");
|
56
|
+
rstr=rstr.gsub("零仟", "零");
|
57
|
+
rstr=rstr.gsub("零萬", "萬");
|
58
|
+
for i in 1..6 do
|
59
|
+
rstr=rstr.gsub("零零", "零");
|
60
|
+
rstr=rstr.gsub("零萬", "零");
|
61
|
+
rstr=rstr.gsub("零億", "億");
|
62
|
+
rstr=rstr.gsub("零零", "零");
|
63
|
+
end
|
64
|
+
rstr=rstr.gsub("零角", "零");
|
65
|
+
rstr=rstr.gsub("零分", "");
|
66
|
+
rstr=rstr.gsub("零元", "");
|
67
|
+
end
|
68
|
+
|
69
|
+
require 'net/http'
|
70
|
+
require 'net/https'
|
71
|
+
require 'uri'
|
72
|
+
|
73
|
+
# get请求
|
74
|
+
# 参数url:网址,header:请求头json类型如:{'Accept' => 'application/json', 'X-Requested-With' => 'XMLHttpRequest'}
|
75
|
+
# 返回response对象,response.code 状态200/304/404;response.body 内容
|
76
|
+
def self.get(url, params=nil, header=nil)
|
77
|
+
uri = URI.parse(url)
|
78
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
79
|
+
http.use_ssl = true if uri.scheme == 'https'
|
80
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
81
|
+
if !params.blank?
|
82
|
+
request.form_data = params
|
83
|
+
end
|
84
|
+
if !header.nil?
|
85
|
+
request.initialize_http_header(header)
|
86
|
+
end
|
87
|
+
http.request(request)
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.post(url, params=nil, header=nil)
|
91
|
+
uri = URI.parse(url)
|
92
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
93
|
+
http.use_ssl = true if uri.scheme == 'https'
|
94
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
95
|
+
if !params.blank?
|
96
|
+
request.set_form_data(params)
|
97
|
+
end
|
98
|
+
if !header.nil?
|
99
|
+
request.initialize_http_header(header)
|
100
|
+
end
|
101
|
+
http.request(request)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Struggle
|
2
|
+
class Pager
|
3
|
+
def initialize(dqyclass='page-navigator-current PNNW-S', aclass='page-navigator-number PNNW-S',
|
4
|
+
spageclass='page-navigator-prev', xpageclass='page-navigator-next', spagedis='page-navigator-prev-disable',
|
5
|
+
xpagedis='page-navigator-next-disable', dianclass='page-navigator-dots')
|
6
|
+
@dqyclass = dqyclass
|
7
|
+
@aclass = aclass
|
8
|
+
@spageclass = spageclass
|
9
|
+
@xpageclass = xpageclass
|
10
|
+
@spagedis = spagedis
|
11
|
+
@xpagedis = xpagedis
|
12
|
+
@dianclass = dianclass
|
13
|
+
end
|
14
|
+
|
15
|
+
def getpage(currentpage, pagecount, route)
|
16
|
+
shenglvhao = "<span class='#{@dianclass}'>...</span>"
|
17
|
+
syiye = currentpage>1 ? "<a class='#{@spageclass}' href='#{route}#{currentpage-1}'><上一页</a>" : "<span class='#{@spagedis}'><上一页</span>"
|
18
|
+
xyiye = currentpage<pagecount ? "<a class='#{@xpageclass}' href='#{route}#{currentpage+1}'>下一页></a>" : "<span class='#{@xpagedis}'><下一页</span>"
|
19
|
+
pages=""
|
20
|
+
if currentpage<7 && pagecount>10
|
21
|
+
for i in 1..9
|
22
|
+
if currentpage==i
|
23
|
+
pages += "<span class='#{@dqyclass}'>#{currentpage}</span>"
|
24
|
+
else
|
25
|
+
pages += "<a class='#{@aclass}' href='#{route}#{i}'>#{i}</a>"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
pages += shenglvhao
|
29
|
+
pages += "<a class='#{@aclass}' href='#{route}#{pagecount}'>#{pagecount}</a>"
|
30
|
+
elsif currentpage>=7 && currentpage<=pagecount-7
|
31
|
+
pages = "<a class='#{@aclass}' href='1'>1</a>"
|
32
|
+
pages += shenglvhao
|
33
|
+
pages += "<a class='#{@aclass}S' href='#{route}#{currentpage-4}'>#{currentpage-4}</a>"
|
34
|
+
pages += "<a class='#{@aclass}' href='#{route}#{currentpage-3}'>#{currentpage-3}</a>"
|
35
|
+
pages += "<a class='#{@aclass}' href='#{route}#{currentpage-2}'>#{currentpage-2}</a>"
|
36
|
+
pages += "<a class='#{@aclass}' href='#{route}#{currentpage-1}'>#{currentpage-1}</a>"
|
37
|
+
pages += "<span class='#{@dqyclass}'>#{currentpage}</span>"
|
38
|
+
pages += "<a class='#{@aclass}' href='#{route}#{currentpage+1}'>#{currentpage+1}</a>"
|
39
|
+
pages += "<a class='#{@aclass}' href='#{route}#{currentpage+2}'>#{currentpage+2}</a>"
|
40
|
+
pages += "<a class='#{@aclass}' href='#{route}#{currentpage+3}'>#{currentpage+3}</a>"
|
41
|
+
pages += "<a class='#{@aclass}' href='#{route}#{currentpage+4}'>#{currentpage+4}</a>"
|
42
|
+
pages += shenglvhao
|
43
|
+
pages += "<a class='#{@aclass}' href='#{route}#{pagecount}'>#{pagecount}</a>"
|
44
|
+
elsif currentpage>=7 && currentpage>pagecount-7
|
45
|
+
pages = "<a class='#{@aclass}' href='1'>1</a>"
|
46
|
+
pages += shenglvhao
|
47
|
+
for i in 1..9
|
48
|
+
fpage = pagecount-9+i
|
49
|
+
if currentpage == fpage
|
50
|
+
pages += "<span class='#{@dqyclass}'>#{currentpage}</span>"
|
51
|
+
else
|
52
|
+
pages += "<a class='#{@aclass}' href='#{route}#{fpage}'>#{fpage}</a>"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
elsif currentpage>0 && pagecount<=10
|
56
|
+
for i in 1..10
|
57
|
+
if currentpage==i
|
58
|
+
pages += "<span class='#{@dqyclass}'>#{currentpage}</span>"
|
59
|
+
else
|
60
|
+
pages += "<a class='#{@aclass}' href='#{route}#{i}'>#{i}</a>"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
syiye + pages +xyiye
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
#gemfile中加入 gem 'iconv'
|
2
|
+
module Struggle
|
3
|
+
class PayGateway
|
4
|
+
require 'net/https'
|
5
|
+
require 'uri'
|
6
|
+
# 支付接口地址
|
7
|
+
URL_UTF8 = "https://pay3.chinabank.com.cn/PayGate?encoding=UTF-8"
|
8
|
+
# 图片和css文件地址
|
9
|
+
URL = "https://pay3.chinabank.com.cn"
|
10
|
+
# 功能:网银在线,网管支付;
|
11
|
+
# 参数:
|
12
|
+
# v_mid:商户编号
|
13
|
+
# key:商户md5密钥
|
14
|
+
# v_url:支付成功后跳转的页面
|
15
|
+
# remark2:异步订单状态地址
|
16
|
+
# v_oid:订单编号
|
17
|
+
# v_amount:订单金额
|
18
|
+
# v_moneytype:支付货币类型
|
19
|
+
def self.pay(v_mid, key, v_url, remark2, v_oid, v_amount, v_moneytype = "CNY")
|
20
|
+
v_md5info = Digest::MD5.hexdigest(v_amount.to_s + v_moneytype + v_oid + v_mid + v_url + key).upcase
|
21
|
+
response = Func.post(URL_UTF8, {'v_mid' => v_mid, 'v_oid' => v_oid, 'v_amount' => v_amount, 'v_moneytype' => v_moneytype, 'v_url' => v_url, 'v_md5info' => v_md5info, 'remark2' => remark2})
|
22
|
+
html = Iconv.iconv('utf-8', 'gbk', response.body).first
|
23
|
+
html.scan(/[href|src]=("[^"]*.[css|js|gif|jpg]")/).each do |h|
|
24
|
+
html = html.gsub(h[0], URL+eval(h[0]).gsub('..', ''))
|
25
|
+
end
|
26
|
+
html
|
27
|
+
end
|
28
|
+
|
29
|
+
# 功能:自动验证结果
|
30
|
+
# 参数:
|
31
|
+
# params参数
|
32
|
+
# key商户的md5密钥
|
33
|
+
# 返回:如果支付成功返回订单号,否则返回nil
|
34
|
+
def self.receive(params, key)
|
35
|
+
v_oid = params["v_oid"]
|
36
|
+
v_pstatus = params["v_pstatus"]
|
37
|
+
v_md5str = params["v_md5str"] #该参数的MD5字符串的顺序为:v_oid,v_pstatus,v_amount,v_moneytype,key
|
38
|
+
v_amount = params["v_amount"]
|
39
|
+
v_moneytype = params["v_moneytype"]
|
40
|
+
# 20(表示支付成功)
|
41
|
+
# 30(表示支付失败)
|
42
|
+
if v_pstatus=="20" && v_md5str==Digest::MD5.hexdigest(v_oid+v_pstatus+v_amount+v_moneytype+key)
|
43
|
+
return params["v_oid"]
|
44
|
+
else
|
45
|
+
return nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/struggle/sms.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
module Struggle
|
2
|
+
class Sms
|
3
|
+
URL = "http://api.cnsms.cn/"
|
4
|
+
require 'net/https'
|
5
|
+
require 'uri'
|
6
|
+
|
7
|
+
def Sms.send(uid, pwd, mobile, content)
|
8
|
+
response = Func.post(URL, {'ac' => "send", 'uid' => uid, 'pwd' => pwd, 'mobile' => mobile, 'content' => content})
|
9
|
+
return response.body=="100"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Struggle
|
2
|
+
class Tmagick
|
3
|
+
#画图类
|
4
|
+
#img->底图 "#{Rails.root}/public/images/1.jpg"
|
5
|
+
#newimg->处理完保存的地址 save img handle path example->"#{Rails.root}/public/images/new.jpg"
|
6
|
+
#example-> Tmagick.new("1.jpg","public/n.jpg").images(images),texts(texts).images(imgs).write
|
7
|
+
def initialize(img, newimg=nil)
|
8
|
+
@pushimg = Magick::Image.read(img).first
|
9
|
+
@poppath = newimg.blank? ? img : newimg
|
10
|
+
end
|
11
|
+
|
12
|
+
#图片叠加和水印
|
13
|
+
#images->多图片叠加,数组,在一个图片上画多个图片; image=>{img: "1.jpg", x: 0, y: 0},img要画的图片地址,xy画的左顶点位置
|
14
|
+
#return->成功返回当前对象,失败返回nil
|
15
|
+
def images(images)
|
16
|
+
return nil if images.blank?
|
17
|
+
images.each do |img|
|
18
|
+
image = Magick::Image.read(img[:img]).first.resize_to_fit(img[:w], img[:h])
|
19
|
+
@pushimg.composite!(image, img[:x], img[:y], Magick::OverCompositeOp)
|
20
|
+
end
|
21
|
+
return self
|
22
|
+
end
|
23
|
+
|
24
|
+
#图片上写字
|
25
|
+
#fontobjs 文字数组; fontobj=>{text: "aaaaaaaaa",color: "#000000",font: "simsun.ttc'",size: 20,weight: 100, x: 0, y: 0}
|
26
|
+
# text文字内容,color颜色,font字体,size大小,weight粗细,xy画的左顶点位置
|
27
|
+
#return-》成功返回当前对象,失败返回nil
|
28
|
+
def texts(texts)
|
29
|
+
return nil if texts.blank?
|
30
|
+
texts.each do |text|
|
31
|
+
copyright = Magick::Draw.new
|
32
|
+
copyright.font = text[:font]
|
33
|
+
copyright.pointsize = text[:size]
|
34
|
+
copyright.font_weight = text[:weight]
|
35
|
+
copyright.fill = text[:color]
|
36
|
+
copyright.annotate(@pushimg, 0, 0, text[:x], text[:y]+12, text[:text])
|
37
|
+
end
|
38
|
+
return self
|
39
|
+
end
|
40
|
+
|
41
|
+
#重定义图片大小
|
42
|
+
def resize(new_width, new_height)
|
43
|
+
@pushimg = Magick::Image.read(@pushimg).first.resize_to_fit(new_width, new_height)
|
44
|
+
@pushimg.blank? ? nil : self
|
45
|
+
end
|
46
|
+
|
47
|
+
#写出操作
|
48
|
+
def write
|
49
|
+
@pushimg.write(@poppath)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
#first gem 'rmagick'
|
2
|
+
module Struggle
|
3
|
+
class Vcode
|
4
|
+
#controller
|
5
|
+
#def getcode
|
6
|
+
# vcode = Vcode.getvcode
|
7
|
+
# session[:code]=vcode[:code]
|
8
|
+
# send_data vcode[:img], content_type: 'image/jpeg', disposition: 'inline'
|
9
|
+
#end
|
10
|
+
#view
|
11
|
+
# <img src="/getcode">
|
12
|
+
#Vcode.getvcode(90,40,24)
|
13
|
+
#w宽,h高,fontsize字体大小
|
14
|
+
#返回json类型
|
15
|
+
def self.getvcode(w=90, h=40, fontsize=24)
|
16
|
+
#创建画布
|
17
|
+
img = Magick::Image.new(w, h) {
|
18
|
+
self.background_color = 'white'
|
19
|
+
self.format="JPG"
|
20
|
+
}
|
21
|
+
text= Magick::Draw.new
|
22
|
+
text.pointsize = fontsize
|
23
|
+
text.kerning = -1
|
24
|
+
text.fill('blue')
|
25
|
+
#随机文字
|
26
|
+
code=""
|
27
|
+
4.times { code << (97 + rand(26)).chr }
|
28
|
+
#设置文字
|
29
|
+
text.text(rand(w/2-5), h/2-5+ rand(h/2), code)
|
30
|
+
#随机直线
|
31
|
+
for i in 1..rand(4)
|
32
|
+
text.line(rand(w), rand(h), rand(w), rand(h)) #直线
|
33
|
+
end
|
34
|
+
text.fill('blue')
|
35
|
+
#燥点
|
36
|
+
for i in 1..280
|
37
|
+
text.point(rand(w), rand(h))
|
38
|
+
end
|
39
|
+
text.draw(img)
|
40
|
+
#模糊
|
41
|
+
img = img.sketch(0, 10, 50)
|
42
|
+
#扭曲
|
43
|
+
img = img.wave(5.5, 50)
|
44
|
+
#返回图片数据流
|
45
|
+
{img: img.to_blob, code: code}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/struggle.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
module Struggle
|
2
|
+
end
|
3
|
+
begin
|
4
|
+
require 'rails'
|
5
|
+
rescue LoadError
|
6
|
+
#do nothing
|
7
|
+
end
|
8
|
+
require 'struggle/fileupload'
|
9
|
+
require 'struggle/func'
|
10
|
+
require 'struggle/pager'
|
11
|
+
require 'struggle/pay_gateway'
|
12
|
+
require 'struggle/sms'
|
13
|
+
require 'struggle/tmagick'
|
14
|
+
require 'struggle/vcode'
|
data/struggle.gemspec
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'struggle'
|
3
|
+
s.version = '1.0.1'
|
4
|
+
s.date = '2015-06-20'
|
5
|
+
s.summary = "struggle!"
|
6
|
+
s.description = "vcode,fileupload,pager,func,pay_gateway,tmagick;"
|
7
|
+
s.authors = ["lean"]
|
8
|
+
s.email = '54850915@qq.com'
|
9
|
+
s.files = `git ls-files`.split("\n")
|
10
|
+
s.extra_rdoc_files = ['README.md']
|
11
|
+
s.require_paths = ['lib']
|
12
|
+
s.add_development_dependency 'iconv', ['>= 0']
|
13
|
+
s.add_development_dependency 'rmagick', ['>= 0']
|
14
|
+
s.homepage = 'http://rubygems.org/gems/struggle'
|
15
|
+
s.license = 'struggle'
|
16
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: struggle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lean
|
@@ -14,28 +14,28 @@ dependencies:
|
|
14
14
|
name: iconv
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rmagick
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
description: vcode,fileupload,pager,func,pay_gateway,tmagick;
|
@@ -46,6 +46,15 @@ extra_rdoc_files:
|
|
46
46
|
- README.md
|
47
47
|
files:
|
48
48
|
- README.md
|
49
|
+
- lib/struggle.rb
|
50
|
+
- lib/struggle/fileupload.rb
|
51
|
+
- lib/struggle/func.rb
|
52
|
+
- lib/struggle/pager.rb
|
53
|
+
- lib/struggle/pay_gateway.rb
|
54
|
+
- lib/struggle/sms.rb
|
55
|
+
- lib/struggle/tmagick.rb
|
56
|
+
- lib/struggle/vcode.rb
|
57
|
+
- struggle.gemspec
|
49
58
|
homepage: http://rubygems.org/gems/struggle
|
50
59
|
licenses:
|
51
60
|
- struggle
|
@@ -56,17 +65,17 @@ require_paths:
|
|
56
65
|
- lib
|
57
66
|
required_ruby_version: !ruby/object:Gem::Requirement
|
58
67
|
requirements:
|
59
|
-
- -
|
68
|
+
- - '>='
|
60
69
|
- !ruby/object:Gem::Version
|
61
70
|
version: '0'
|
62
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
72
|
requirements:
|
64
|
-
- -
|
73
|
+
- - '>='
|
65
74
|
- !ruby/object:Gem::Version
|
66
75
|
version: '0'
|
67
76
|
requirements: []
|
68
77
|
rubyforge_project:
|
69
|
-
rubygems_version: 2.4.
|
78
|
+
rubygems_version: 2.4.8
|
70
79
|
signing_key:
|
71
80
|
specification_version: 4
|
72
81
|
summary: struggle!
|