Class: Image

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, path, output_path) ⇒ Image

Returns a new instance of Image.



4
5
6
7
8
# File 'lib/image.rb', line 4

def initialize(id, path, output_path)
  @id = id
  @path = path
  @output_path = output_path
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



2
3
4
# File 'lib/image.rb', line 2

def id
  @id
end

#pathObject (readonly)

Returns the value of attribute path.



2
3
4
# File 'lib/image.rb', line 2

def path
  @path
end

Instance Method Details

#basenameObject



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

def basename
  File.basename(@path)
end

#resize(size, square = false) ⇒ Object



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

def resize(size, square = false)
  thumb_file_path = File.join(@output_path, "thumbs", File.basename(@path, ".jpg") << "_#{size}.jpg")

  unless File.exists?(thumb_file_path)
    file = Magick::Image::read(@path).first
  
    if square
      file.resize_to_fill!(size, size)
    else
      file.resize_to_fit!(size, size)
    end

    file.write(thumb_file_path){ self.quality = (size <= 200 ? 75 : 95) }
    
    # Free memory
    file.destroy!
  end

  File.join("thumbs", File.basename(thumb_file_path))
end

#to_sObject



35
36
37
# File 'lib/image.rb', line 35

def to_s
  basename
end