Class: Riiif::File

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Benchmarkable, Open3
Defined in:
app/models/riiif/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_path, tempfile = nil) ⇒ File

Returns a new instance of File.

Parameters:

  • input_path (String)

    The location of an image file



12
13
14
15
# File 'app/models/riiif/file.rb', line 12

def initialize(input_path, tempfile = nil)
  @path = input_path
  @tempfile = tempfile # ensures that the tempfile will stick around until this file is garbage collected.
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'app/models/riiif/file.rb', line 7

def path
  @path
end

Class Method Details

.create(ext = nil, _validate = true, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/riiif/file.rb', line 25

def self.create(ext = nil, _validate = true, &block)

  tempfile = Tempfile.new(['mini_magick', ext.to_s.downcase])
  tempfile.binmode
  block.call(tempfile)
  tempfile.close
  image = new(tempfile.path, tempfile)
ensure
  tempfile.close if tempfile

end

.read(stream, ext) ⇒ Object



17
18
19
20
21
22
23
# File 'app/models/riiif/file.rb', line 17

def self.read(stream, ext)
  create(ext) do |f|
    while chunk = stream.read(8192)
      f.write(chunk)
    end
  end
end

Instance Method Details

#extract(options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/riiif/file.rb', line 37

def extract(options)

  command = 'convert'
  command << " -crop #{options[:crop]}" if options[:crop]
  command << " -resize #{options[:size]}" if options[:size]
  if options[:rotation]
    command << " -virtual-pixel white +distort srt #{options[:rotation]}"
  end

  case options[:quality]
  when 'grey'
    command << ' -colorspace Gray'
  when 'bitonal'
    command << ' -colorspace Gray'
    command << ' -type Bilevel'
  end
  command << " #{path} #{options[:format]}:-"
  execute(command)
end

#infoObject



57
58
59
60
61
# File 'app/models/riiif/file.rb', line 57

def info
  return @info if @info
  height, width = execute("identify -format %hx%w #{path}").split('x')
  @info = { height: Integer(height), width: Integer(width) }
end