Class: Imanip::Interface::Magick

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Magick

Returns a new instance of Magick.



12
13
14
15
# File 'lib/imanip/imanip_magick.rb', line 12

def initialize(path)
  @image_path = path
  identify
end

Class Attribute Details

.execute_pathObject

Returns the value of attribute execute_path.



7
8
9
# File 'lib/imanip/imanip_magick.rb', line 7

def execute_path
  @execute_path
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



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

def format
  @format
end

#heightObject (readonly) Also known as: rows

Returns the value of attribute height.



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

def height
  @height
end

#image_pathObject (readonly)

Returns the value of attribute image_path.



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

def image_path
  @image_path
end

#widthObject (readonly) Also known as: columns

Returns the value of attribute width.



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

def width
  @width
end

Instance Method Details

#convert(to_file, options = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/imanip/imanip_magick.rb', line 102

def convert(to_file,options = {})
  command = "#{execute_path}convert #{@image_path} #{options_to_string(options)} #{to_file}"
  response = execute(command)
  # catch errors in response
  possible_errors = [
    /invalid argument/
  ]
  possible_errors.each do |error|
    raise(CouldNotConvertError, response + " when " + command) if response =~ error
  end
  # assert that the new file exits
  File.readable?(to_file) ? Imanip::Image.new(to_file, :magick) : raise(CouldNotConvertError)
end

#crop(to_file, options = {}) ⇒ Object



55
56
57
58
59
60
# File 'lib/imanip/imanip_magick.rb', line 55

def crop(to_file, options = {})
  @options = options
  parse_size_options!
  @options[:crop] = to_geometry_string(@geometry)
  convert(to_file, options)
end

#crop_resize(to_file, options = {}) ⇒ Object Also known as: crop_resized



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/imanip/imanip_magick.rb', line 69

def crop_resize(to_file, options = {})
  @options = options.dup
  parse_size_options!
  crop_resize_string = ""
  crop_width = @geometry[:width]
  crop_height =  @geometry[:height]
  if !(crop_height.nil? || crop_width.nil?)
    crop_ratio = crop_width.to_f / crop_height.to_f
    if crop_ratio > ratio
      crop_resize_string << "-resize #{to_geometry_string(:width => crop_width)}"
    else
      crop_resize_string << "-resize #{to_geometry_string(:height => crop_height)}"
    end
  else
    crop_resize_string << "-resize #{to_geometry_string(:height => crop_height, :width => crop_width)}"
  end
  gravity = @options.delete(:gravity) || 'North'
  crop_resize_string << " -gravity #{gravity}"
  crop_resize_string << " -crop #{to_geometry_string(@geometry)}+0+0"
  crop_resize_string << " #{options_to_string(@options)}"
  convert(to_file,crop_resize_string)
end

#dimensionsObject

Return the dimensions of the image as an array of Fixnums [width,height]



21
22
23
# File 'lib/imanip/imanip_magick.rb', line 21

def dimensions
  [width,height]
end

#identify(options = {}) ⇒ Object

Raises:



93
94
95
96
97
98
99
100
# File 'lib/imanip/imanip_magick.rb', line 93

def identify(options = {})
  response = execute("#{execute_path}identify #{options_to_string(options)} #{@image_path}")
  matches = response.match(/(JPEG|PNG|GIF)\ (\d+)x(\d+)/)
  raise NotAnImageError, "Could not identify the image #{@image_path} as an image: #{response}" if matches.nil?
  @format = matches[1]
  @width = matches[2].to_i
  @height = matches[3].to_i
end

#landscape?Boolean

Returns true if the image is longer then it is tall

Returns:

  • (Boolean)


31
32
33
# File 'lib/imanip/imanip_magick.rb', line 31

def landscape?
  width > height
end

#orientationObject

Returns symbol of the images orientation. Can be :landscape, :portrait, or :square



41
42
43
44
45
46
47
48
49
# File 'lib/imanip/imanip_magick.rb', line 41

def orientation
  if landscape?
    :landscape
  elsif portrait?
    :portrait
  else
    :square
  end
end

#portrait?Boolean

Returns true if the image is taller then it is long

Returns:

  • (Boolean)


26
27
28
# File 'lib/imanip/imanip_magick.rb', line 26

def portrait?
  width < height
end

#ratioObject



51
52
53
# File 'lib/imanip/imanip_magick.rb', line 51

def ratio
  width.to_f / height.to_f
end

#resize(to_file, options = {}) ⇒ Object



62
63
64
65
66
67
# File 'lib/imanip/imanip_magick.rb', line 62

def resize(to_file, options = {})
  @options = options
  parse_size_options!
  @options[:resize] = to_geometry_string(@geometry)
  convert(to_file, @options)
end

#square?Boolean

Returns true if width == height

Returns:

  • (Boolean)


36
37
38
# File 'lib/imanip/imanip_magick.rb', line 36

def square?
  width == height
end