imageupload 0.1.3

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d6f4255ae65a4606dda644020f4ed4d6e2b8f6be
4
+ data.tar.gz: fcceba5bdd99935fcf51bd1adc7f6c9e2acd9616
5
+ SHA512:
6
+ metadata.gz: 4d64c38d3f39e836f4a6ba563959d91fe87f5073aad920fe875e6a061f55ccc05dd745212517cdfc631a6f81debe167661bc2da9511615bcaf0c2aebe417f113
7
+ data.tar.gz: 5e8875237df6e9200256566be255f79f1bfa0a8c5dd397159411de4f13bfe5b573f663ff9417a0e3e55205d8eabac0ea987c4719f7de073dea66659070b8a093
data/lib/fileupload.rb ADDED
@@ -0,0 +1,50 @@
1
+ class Fileupload
2
+ def initialize(filepath="/", format="jpg|gif",min=10,max=400)
3
+ @filepath=filepath
4
+ @format=format
5
+ @message=""
6
+ @minsize =min*1024
7
+ @maxsize=max*1024
8
+ end
9
+
10
+ def upload(file, rw=0, rh=0, dw=0, dh=0)
11
+ if self.img_validata(file)
12
+ sname = self.getname(file.original_filename)
13
+ File.open(@filepath+sname, "wb") do |f|
14
+ f.write(file.read)
15
+ end
16
+ sname
17
+ end
18
+ end
19
+
20
+ #大小限制,类型限制,都放进模型层中。
21
+ def img_validata(file)
22
+ img_for = /\.(@format)+$/.match(file.original_filename)
23
+ if img_for.nil? || img_for.blank?
24
+ @message="错误:图片格式不对,只允许上传jpg,gif,png,bmp,jpeg格式!\\n"
25
+ return false
26
+ end
27
+ if file.size<@minsize || file.size>@maxsize
28
+ @message="错误:图片大小错误,只允许#{@minsize}kb~#{@maxsize}kb!\\n"
29
+ return false
30
+ end
31
+ return true
32
+ end
33
+
34
+ #错误提示
35
+ def message
36
+ @message
37
+ end
38
+
39
+ #保证名称不重复
40
+ def getname(filestream)
41
+ img_for = filestream[filestream.length-4, 4]
42
+ filename = Time.now.strftime("%Y%m%d%h%m%s")<<rand(99999).to_s<<img_for
43
+ file = @filepath+filename
44
+ while File.exist?(file) do
45
+ filename = Time.now.strftime("%Y%m%d%h%m%s")<<rand(99999).to_s<<img_for
46
+ file = @filepath+filename
47
+ end
48
+ filename
49
+ end
50
+ end
@@ -0,0 +1,107 @@
1
+ class Imageupload
2
+ #初始化
3
+ #临时路径
4
+ #图片路径
5
+ #清理垃圾,=0不清理
6
+ def initialize(minsize=10, maxsize=400, snappath="app/assets/images/snap/", imagepath="app/assets/images/upload/", cleartime=10, bigpath="app/assets/images/upload/")
7
+ @minsize = minsize*1024
8
+ @maxsize = maxsize*1024
9
+ @snappath = snappath
10
+ @imagepath = imagepath
11
+ @cleartime = cleartime
12
+ @bigpath = bigpath
13
+ @message=""
14
+ end
15
+
16
+ #忽略高宽就是自定义大小,传原图到临时空间
17
+ def upload(imgfile, rw=0, rh=0, dw=0, dh=0)
18
+ if self.img_validata(imgfile)
19
+ sname = self.getname(imgfile.original_filename)
20
+ imgpath = rw==0 || rh==0 ? @snappath+sname : @imagepath+sname
21
+ File.open(imgpath, "wb") do |f|
22
+ f.write(imgfile.read)
23
+ end
24
+ if @cleartime>0
25
+ self.clearsnap(sname)
26
+ end
27
+ if rw==0 || rh==0
28
+ return sname
29
+ else
30
+ self.resize(rw, rh, sname, dw, dh)
31
+ end
32
+ end
33
+ end
34
+
35
+ #自定义剪裁缩放
36
+ def reimage(x, y, w, h, rw, rh, pname)
37
+ imgsnap = @snappath+pname
38
+ imgp = @imagepath+pname
39
+ img = Magick::Image.read(imgsnap)[0]
40
+ if w==0 || h==0
41
+ rc = img.resize(rw, rh)
42
+ rc.write(imgp)
43
+ else
44
+ chopped = img.crop(x, y, w, h)
45
+ rc = chopped.resize(rw, rh)
46
+ rc.write(imgp)
47
+ end
48
+ File.delete(imgsnap)
49
+ end
50
+
51
+ #错误提示
52
+ def message
53
+ @message
54
+ end
55
+
56
+ #固定剪裁缩放
57
+ def resize(rw, rh, imgname, dw, dh)
58
+ img = Magick::Image.read(@imagepath+imgname)[0]
59
+ newimg = img.resize_to_fill(rw, rh)
60
+ newimg.write(@imagepath+imgname)
61
+ if dw!=0 && dh !=0
62
+ name= imgname[0, imgname.length-4]
63
+ format= imgname[imgname.length-4, 4]
64
+ bigimg=img.resize_to_fill(dw, dh)
65
+ bigimg.write(@bigpath+name+"b"+format)
66
+ end
67
+ end
68
+
69
+ #大小限制,类型限制,都放进模型层中。
70
+ def img_validata(img_file)
71
+
72
+ img_for = /\.(jpg|gif|GIF|png|jpeg|bmp|BMP|JPG|JPEG|PNG)+$/.match(img_file.original_filename)
73
+ if img_for.nil? || img_for.blank?
74
+ @message="错误:图片格式不对,只允许上传jpg,gif,png,bmp,jpeg格式!\\n"
75
+ return false
76
+ end
77
+ if img_file.size<@minsize || img_file.size>@maxsize
78
+ @message="错误:图片大小错误,只允许#{@minsize}kb~#{@maxsize}kb!\\n"
79
+ return false
80
+ end
81
+ return true
82
+ end
83
+
84
+ #保证名称不重复
85
+ def getname(img_file)
86
+ img_for = img_file[img_file.length-4, 4]
87
+ filename = Time.now.strftime("%Y%m%d%h%m%s")<<rand(99999).to_s<<img_for
88
+ file = @imagepath+filename
89
+ while File.exist?(file) do
90
+ filename = Time.now.strftime("%Y%m%d%h%m%s")<<rand(99999).to_s<<img_for
91
+ file = @imagepath+filename
92
+ end
93
+ filename
94
+ end
95
+
96
+ #删除原图
97
+ def clearsnap(snapname)
98
+ Thread.new do
99
+ snapimg = @snappath+snapname
100
+ sleep @cleartime
101
+ if File.exist?(snapimg)
102
+ File.delete(snapimg)
103
+ end
104
+ Thread.current.kill
105
+ end
106
+ end
107
+ end
data/lib/vcode.rb ADDED
@@ -0,0 +1,40 @@
1
+ class Vcode
2
+ def initialize(w=90,h=40)
3
+ @w=w and @h=h
4
+ end
5
+ def getvcode
6
+ #创建画布
7
+ img = Magick::Image.new(@w, @h) {
8
+ self.background_color = 'white'
9
+ self.format="JPG"
10
+ }
11
+ text= Magick::Draw.new
12
+ text.pointsize = 24
13
+ text.kerning = -1
14
+ text.fill('blue')
15
+ #随机文字
16
+ @code=""
17
+ 4.times{@code << (97 + rand(26)).chr}
18
+ #设置文字
19
+ text.text(rand(@w/2-5),@h/2-5+ rand(@h/2), @code)
20
+ #随机直线
21
+ for i in 1..rand(4)
22
+ text.line(rand(@w), rand(@h), rand(@w), rand(@h)) #直线
23
+ end
24
+ text.fill('blue')
25
+ #燥点
26
+ for i in 1..280
27
+ text.point(rand(@w), rand(@h))
28
+ end
29
+ text.draw(img)
30
+ #模糊
31
+ img = img.sketch(0, 10, 50)
32
+ #扭曲
33
+ img = img.wave(5.5, 50)
34
+ #返回图片数据流
35
+ img.to_blob
36
+ end
37
+ def code
38
+ @code
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imageupload
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Leanv
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A image upload rb lib
14
+ email: leanv@sina.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/imageupload.rb
20
+ - lib/fileupload.rb
21
+ - lib/vcode.rb
22
+ homepage: http://rubygems.org/gems/imageupload
23
+ licenses:
24
+ - Lean
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.0.3
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: image upload
46
+ test_files: []