Class: Imgry::Processor::MiniMagick

Inherits:
Adapter
  • Object
show all
Defined in:
lib/imgry/processor/mini_magick.rb

Instance Attribute Summary

Attributes inherited from Adapter

#img, #img_blob

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Adapter

#aspect_ratio, #initialize, #inspect, load_lib!

Constructor Details

This class inherits a constructor from Imgry::Processor::Adapter

Class Method Details

.from_file(path) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/imgry/processor/mini_magick.rb', line 10

def self.from_file(path)
  if !File.readable?(path)
    raise FileUnreadableError, path.to_s
  end

  new(IO.read(path))
end

.with_bytes(img_blob) ⇒ Object



6
7
8
# File 'lib/imgry/processor/mini_magick.rb', line 6

def self.with_bytes(img_blob)
  new(img_blob)
end

Instance Method Details

#clean!Object



71
72
73
74
75
# File 'lib/imgry/processor/mini_magick.rb', line 71

def clean!
  @img.destroy!
  @img = nil
  @img_blob = nil
end

#crop!(geometry) ⇒ Object



33
34
35
36
37
# File 'lib/imgry/processor/mini_magick.rb', line 33

def crop!(geometry)
  return if geometry.nil?
  @img.crop(geometry)
  nil
end

#formatObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/imgry/processor/mini_magick.rb', line 47

def format
  @format ||= begin
    format = @img['format']
    if !format.nil?
      # Normalize..
      format.downcase!
      format == 'jpeg' ? 'jpg' : format
    end
  end
end

#heightObject



43
44
45
# File 'lib/imgry/processor/mini_magick.rb', line 43

def height
  @img['height']
end

#load_image!Object



18
19
20
21
22
23
24
25
# File 'lib/imgry/processor/mini_magick.rb', line 18

def load_image!
  begin
    @format = nil
    @img = ::MiniMagick::Image.read(@img_blob)
  rescue ::MiniMagick::Invalid => ex
    raise InvalidImageError, ex.message
  end
end

#resize!(geometry) ⇒ Object



27
28
29
30
31
# File 'lib/imgry/processor/mini_magick.rb', line 27

def resize!(geometry)
  return if geometry.nil?
  @img.resize(geometry)
  nil
end

#save(path) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/imgry/processor/mini_magick.rb', line 63

def save(path)
  if !File.writable?(File.dirname(path))
    raise FileUnwritableError, path.to_s
  end
  # TODO: error checking on write
  @img.write(path.to_s)
end

#to_blob(format = nil) ⇒ Object



58
59
60
61
# File 'lib/imgry/processor/mini_magick.rb', line 58

def to_blob(format=nil)
  # TODO: support other output format
  @img.to_blob
end

#widthObject



39
40
41
# File 'lib/imgry/processor/mini_magick.rb', line 39

def width
  @img['width']
end