Class: Imanip::Interface::Magick
- Inherits:
-
Object
- Object
- Imanip::Interface::Magick
- Defined in:
- lib/imanip/imanip_magick.rb
Class Attribute Summary collapse
-
.execute_path ⇒ Object
Returns the value of attribute execute_path.
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#height ⇒ Object
(also: #rows)
readonly
Returns the value of attribute height.
-
#image_path ⇒ Object
readonly
Returns the value of attribute image_path.
-
#width ⇒ Object
(also: #columns)
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #convert(to_file, options = {}) ⇒ Object
- #crop(to_file, options = {}) ⇒ Object
- #crop_resize(to_file, options = {}) ⇒ Object (also: #crop_resized)
-
#dimensions ⇒ Object
Return the dimensions of the image as an array of Fixnums [width,height].
- #identify(options = {}) ⇒ Object
-
#initialize(path) ⇒ Magick
constructor
A new instance of Magick.
-
#landscape? ⇒ Boolean
Returns true if the image is longer then it is tall.
-
#orientation ⇒ Object
Returns symbol of the images orientation.
-
#portrait? ⇒ Boolean
Returns true if the image is taller then it is long.
- #ratio ⇒ Object
- #resize(to_file, options = {}) ⇒ Object
-
#square? ⇒ Boolean
Returns true if width == height.
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_path ⇒ Object
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
#format ⇒ Object (readonly)
Returns the value of attribute format.
10 11 12 |
# File 'lib/imanip/imanip_magick.rb', line 10 def format @format end |
#height ⇒ Object (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_path ⇒ Object (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 |
#width ⇒ Object (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, = {}) command = "#{execute_path}convert #{@image_path} #{()} #{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[:crop] = to_geometry_string(@geometry) convert(to_file, ) 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 = .dup 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)}" convert(to_file,crop_resize_string) end |
#dimensions ⇒ Object
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
93 94 95 96 97 98 99 100 |
# File 'lib/imanip/imanip_magick.rb', line 93 def identify( = {}) response = execute("#{execute_path}identify #{()} #{@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
31 32 33 |
# File 'lib/imanip/imanip_magick.rb', line 31 def landscape? width > height end |
#orientation ⇒ Object
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
26 27 28 |
# File 'lib/imanip/imanip_magick.rb', line 26 def portrait? width < height end |
#ratio ⇒ Object
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[:resize] = to_geometry_string(@geometry) convert(to_file, @options) end |
#square? ⇒ Boolean
Returns true if width == height
36 37 38 |
# File 'lib/imanip/imanip_magick.rb', line 36 def square? width == height end |