Class: Fdimage

Inherits:
Object
  • Object
show all
Defined in:
lib/fdimage.rb,
lib/fdimage/version.rb

Constant Summary collapse

VERSION =
'0.2.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, params: {}) ⇒ Fdimage

Returns a new instance of Fdimage.



13
14
15
16
# File 'lib/fdimage.rb', line 13

def initialize(data, params: {})
  self.archivo = Fdarchivo.new(data)
  self.params = params
end

Instance Attribute Details

#archivoObject

Solo funciona al subir una nueva imagen y solo desde la plataforma, no desde la app TODO:

Recortar archivos subidos


10
11
12
# File 'lib/fdimage.rb', line 10

def archivo
  @archivo
end

#paramsObject

Solo funciona al subir una nueva imagen y solo desde la plataforma, no desde la app TODO:

Recortar archivos subidos


10
11
12
# File 'lib/fdimage.rb', line 10

def params
  @params
end

Class Method Details

.tmp_multi_upload(paths, owner = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/fdimage.rb', line 78

def self.tmp_multi_upload(paths, owner = nil)
  code = Fdarchivo.gen_upload_code
  paths.each do |path|
    a = self.new(path)
    a.fix_orientacion_apple
    a.archivo.tmp_upload(owner, code: code)
  end
  code
end

.tmp_upload(path, owner = nil) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/fdimage.rb', line 87

def self.tmp_upload(path, owner = nil)
  code = Fdarchivo.gen_upload_code
  a = self.new(path)
  a.fix_orientacion_apple
  a.archivo.tmp_upload(owner, code: code)
  code
end

.upload(path, owner = nil) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/fdimage.rb', line 95

def self.upload(path, owner = nil)
  code = Fdarchivo.gen_upload_code
  a = self.new(path)
  a.fix_orientacion_apple
  a.archivo.upload(owner, code: code)
  code
end

Instance Method Details

#convert(parametros: self.params) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fdimage.rb', line 47

def convert(parametros: self.params)
  self.get_tmp_file
  if parametros
    MiniMagick::Tool::Convert.new do |c|
      c << "#{self.archivo.path}"
      if parametros[:rotate]!=0
        c << '-rotate' << "#{parametros[:rotate].to_i}"
      end
      c << '-crop' << "#{parametros[:width].to_f}x#{parametros[:height].to_f}+#{parametros[:x].to_f}+#{parametros[:y].to_f}"
      if parametros[:x].to_f<0 && parametros[:y].to_f<0
        c << '-page' << "#{parametros[:width].to_f}x#{parametros[:height].to_f}+#{parametros[:x].to_f.abs}+#{parametros[:y].to_f.abs}"
      elsif parametros[:y].to_f<0
        c << '-page' << "#{parametros[:width].to_f}x#{parametros[:height].to_f}+0+#{parametros[:y].to_f.abs}"
      elsif parametros[:x].to_f<0
        c << '-page' << "#{parametros[:width].to_f}x#{parametros[:height].to_f}+#{parametros[:x].to_f.abs}+0"
      else
        c << '-page' << "#{parametros[:width].to_f}x#{parametros[:height].to_f}+0+0"
      end
      c << '-format' << 'jpeg'
      c << '-background' << 'white'
      c << '-depth' << '8'
      c << '-density' << '72'
      c << '+channel'
      c << '-flatten'
      c << '-strip'
      c << '-gaussian-blur' << '0.05'
      c << self.archivo.path
    end
  end
end

#fix_orientacion_appleObject



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

def fix_orientacion_apple
  exif = MiniMagick::Image.open(self.archivo.path).exif
  if exif['Orientation']
    MiniMagick::Tool::Convert.new do |c|
      c << self.archivo.path
      case exif['Orientation']
        when '6'
          c.rotate << '90'
        when '8'
          c.rotate << '-90'
        when '3'
          c.rotate << '180'
      end
      c << '-strip'
      c << self.archivo.path
    end
  end
end

#get_tmp_fileObject



37
38
39
40
41
42
43
44
45
# File 'lib/fdimage.rb', line 37

def get_tmp_file
  new_path = "#{Rails.root}/public/tmp/"
  unless Dir.exist?(new_path)
    FileUtils.mkpath(new_path)
  end
  new_path << "#{self.archivo.archivo.original_filename}"
  FileUtils.mv(self.archivo.path, new_path)
  self.archivo.path = new_path
end