Class: Natour::Image

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, image) ⇒ Image

Returns a new instance of Image.


11
12
13
14
15
16
17
18
19
# File 'lib/natour/image.rb', line 11

def initialize(path, image)
  @path = path
  @image = image
  @landscape = image.width >= image.height
  orientation = get_field('exif-ifd0-Orientation')
  @landscape = !@landscape if orientation && orientation[/^(\d) \(/, 1].to_i.between?(5, 8)
  date_time = get_field('exif-ifd0-DateTime')
  @date_time = Timeliness.parse(date_time[/^(.*?) \(/, 1], format: 'yyyy:mm:dd hh:nn:ss') if date_time
end

Instance Attribute Details

#date_timeObject (readonly)

Returns the value of attribute date_time.


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

def date_time
  @date_time
end

#pathObject (readonly)

Returns the value of attribute path.


8
9
10
# File 'lib/natour/image.rb', line 8

def path
  @path
end

Class Method Details

.load_file(filename) ⇒ Object


21
22
23
# File 'lib/natour/image.rb', line 21

def self.load_file(filename)
  Image.new(filename, Vips::Image.new_from_file(filename))
end

Instance Method Details

#autorotateObject


29
30
31
# File 'lib/natour/image.rb', line 29

def autorotate
  Image.new(@path, @image.autorot)
end

#landscape?Boolean

Returns:

  • (Boolean)

25
26
27
# File 'lib/natour/image.rb', line 25

def landscape?
  @landscape
end

#save_as(filename) ⇒ Object


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

def save_as(filename)
  FileUtils.mkdir_p(Pathname(filename).dirname)
  StdoutUtils.suppress_output { @image.write_to_file(filename) }
end

#shrink_to(maxdim) ⇒ Object


33
34
35
36
37
38
39
40
41
# File 'lib/natour/image.rb', line 33

def shrink_to(maxdim)
  scale = maxdim / @image.size.max.to_f
  image = if scale < 1.0
            @image.resize(scale)
          else
            @image.copy
          end
  Image.new(@path, image)
end