Class: EspCommons::Image

Inherits:
APISmith::Smash
  • Object
show all
Defined in:
app/models/esp_commons/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#aspect_ratioObject



38
39
40
# File 'app/models/esp_commons/image.rb', line 38

def aspect_ratio
  @aspect_ratio ||= width.to_f / height
end

Instance Method Details

#as_json(options = {}) ⇒ Object



74
75
76
# File 'app/models/esp_commons/image.rb', line 74

def as_json(options={})
  super(options.merge(:only => %w[url width height description]))
end

#build_urlObject



20
21
22
23
24
# File 'app/models/esp_commons/image.rb', line 20

def build_url
  self.tap do | image |
    image.url = "#{Settings['vfs.url']}/files/#{image.id}/#{image.width}-#{image.height}/#{image.filename}"
  end
end

#create_thumbnail(options) ⇒ Object



67
68
69
70
71
72
# File 'app/models/esp_commons/image.rb', line 67

def create_thumbnail(options)
  return unless image?
  self.thumbnail = EspCommons::Image.new(options.merge(:id => id, :filename => filename, :description => description))
                                    .resize(aspect_ratio)
                                    .build_url
end

#height?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'app/models/esp_commons/image.rb', line 34

def height?
  height && height > 0
end

#image?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'app/models/esp_commons/image.rb', line 26

def image?
  width && height
end

#new_heightObject



59
60
61
# File 'app/models/esp_commons/image.rb', line 59

def new_height
  @new_height ||= width / aspect_ratio
end

#new_widthObject



63
64
65
# File 'app/models/esp_commons/image.rb', line 63

def new_width
  @new_width ||= height * aspect_ratio
end

#parse_urlObject



14
15
16
17
18
# File 'app/models/esp_commons/image.rb', line 14

def parse_url
  self.tap do | image |
    image.id, image.width, image.height, image.filename = url.match(%r{files/(\d+)/(\d+)-(\d+)/(.*)})[1..-1]
  end
end

#resize(aspect_ratio) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/esp_commons/image.rb', line 42

def resize(aspect_ratio)
  self.aspect_ratio = aspect_ratio
  if width? && !height?
    self.height = new_height
  elsif !width? && height?
    self.width = new_width
  else
    self.width = self.height = 100 if !width? && !height?
    if height >= new_height
      self.height = new_height
    else
      self.width = new_width
    end
  end
  self
end

#width?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'app/models/esp_commons/image.rb', line 30

def width?
  width && width > 0
end