Class: Imageupload

Inherits:
Object
  • Object
show all
Defined in:
lib/imageupload.rb

Instance Method Summary collapse

Constructor Details

#initialize(minsize = 10, maxsize = 400, snappath = "app/assets/images/snap/", imagepath = "app/assets/images/upload/", cleartime = 10, bigpath = "app/assets/images/upload/") ⇒ Imageupload

初始化 临时路径 图片路径 清理垃圾,=0不清理



6
7
8
9
10
11
12
13
14
# File 'lib/imageupload.rb', line 6

def initialize(minsize=10, maxsize=400, snappath="app/assets/images/snap/", imagepath="app/assets/images/upload/", cleartime=10, bigpath="app/assets/images/upload/")
  @minsize = minsize*1024
  @maxsize = maxsize*1024
  @snappath = snappath
  @imagepath = imagepath
  @cleartime = cleartime
  @bigpath = bigpath
  @message=""
end

Instance Method Details

#clearsnap(snapname) ⇒ Object

删除原图



97
98
99
100
101
102
103
104
105
106
# File 'lib/imageupload.rb', line 97

def clearsnap(snapname)
  Thread.new do
    snapimg = @snappath+snapname
    sleep @cleartime
    if File.exist?(snapimg)
      File.delete(snapimg)
    end
    Thread.current.kill
  end
end

#getname(img_file) ⇒ Object

保证名称不重复



85
86
87
88
89
90
91
92
93
94
# File 'lib/imageupload.rb', line 85

def getname(img_file)
  img_for = img_file[img_file.length-4, 4]
  filename = Time.now.strftime("%Y%m%d%h%m%s")<<rand(99999).to_s<<img_for
  file = @imagepath+filename
  while File.exist?(file) do
    filename = Time.now.strftime("%Y%m%d%h%m%s")<<rand(99999).to_s<<img_for
    file = @imagepath+filename
  end
  filename
end

#img_validata(img_file) ⇒ Object

大小限制,类型限制,都放进模型层中。



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/imageupload.rb', line 70

def img_validata(img_file)

  img_for = /\.(jpg|gif|GIF|png|jpeg|bmp|BMP|JPG|JPEG|PNG)+$/.match(img_file.original_filename)
  if img_for.nil? || img_for.blank?
    @message="错误:图片格式不对,只允许上传jpg,gif,png,bmp,jpeg格式!\\n"
    return false
  end
  if img_file.size<@minsize || img_file.size>@maxsize
    @message="错误:图片大小错误,只允许#{@minsize}kb~#{@maxsize}kb!\\n"
    return false
  end
  return true
end

#messageObject

错误提示



52
53
54
# File 'lib/imageupload.rb', line 52

def message
  @message
end

#reimage(x, y, w, h, rw, rh, pname) ⇒ Object

自定义剪裁缩放



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/imageupload.rb', line 36

def reimage(x, y, w, h, rw, rh, pname)
  imgsnap = @snappath+pname
  imgp = @imagepath+pname
  img = Magick::Image.read(imgsnap)[0]
  if w==0 || h==0
    rc = img.resize(rw, rh)
    rc.write(imgp)
  else
    chopped = img.crop(x, y, w, h)
    rc = chopped.resize(rw, rh)
    rc.write(imgp)
  end
  File.delete(imgsnap)
end

#resize(rw, rh, imgname, dw, dh) ⇒ Object

固定剪裁缩放



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/imageupload.rb', line 57

def resize(rw, rh, imgname, dw, dh)
  img = Magick::Image.read(@imagepath+imgname)[0]
  newimg = img.resize_to_fill(rw, rh)
  newimg.write(@imagepath+imgname)
  if dw!=0 && dh !=0
    name= imgname[0, imgname.length-4]
    format= imgname[imgname.length-4, 4]
    bigimg=img.resize_to_fill(dw, dh)
    bigimg.write(@bigpath+name+"b"+format)
  end
end

#upload(imgfile, rw = 0, rh = 0, dw = 0, dh = 0) ⇒ Object

忽略高宽就是自定义大小,传原图到临时空间



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/imageupload.rb', line 17

def upload(imgfile, rw=0, rh=0, dw=0, dh=0)
  if self.img_validata(imgfile)
    sname = self.getname(imgfile.original_filename)
    imgpath = rw==0 || rh==0 ? @snappath+sname : @imagepath+sname
    File.open(imgpath, "wb") do |f|
      f.write(imgfile.read)
    end
    if @cleartime>0
      self.clearsnap(sname)
    end
    if rw==0 || rh==0
      return sname
    else
      self.resize(rw, rh, sname, dw, dh)
    end
  end
end