Class: Monet::Image

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Image

Returns a new instance of Image.



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

def initialize(path)
  @path = File.expand_path path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#basenameObject



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

def basename
  @path.split(File::SEPARATOR)[-2..-1].join(File::SEPARATOR)
end

#diff_pathObject



19
20
21
# File 'lib/monet/image.rb', line 19

def diff_path
  @path.gsub(".png", "-diff.png")
end

#flagged?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/monet/image.rb', line 23

def flagged?
  File.exists? diff_path
end

#is_diff?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/monet/image.rb', line 15

def is_diff?
  name.include? "-diff"
end

#nameObject



31
32
33
# File 'lib/monet/image.rb', line 31

def name
  @name ||= File.basename @path
end

#root_dirObject



43
44
45
# File 'lib/monet/image.rb', line 43

def root_dir
  File.dirname @path
end

#thumbnail!(save_to = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/monet/image.rb', line 47

def thumbnail!(save_to=nil)
  img = to_image
  short_edge = [img.width, img.height].min

  cropped = img.crop(0, 0, short_edge, short_edge)
  resized = cropped.resize(200, 200)

  if save_to.nil?
    save_to = @path.gsub(".png", "-thumb.png")
  else
    save_to = File.join save_to, File.basename(@path)
  end

  save_dir = File.dirname(save_to)
  FileUtils.mkdir_p save_dir unless Dir.exists?(save_dir)

  resized.save save_to
  save_to
end

#to_imageObject



39
40
41
# File 'lib/monet/image.rb', line 39

def to_image
  ChunkyPNG::Image.from_file(@path)
end

#widthObject



27
28
29
# File 'lib/monet/image.rb', line 27

def width
  @width ||= File.basename(@path, ".png").split("-").last.to_i
end