Class: ImageProcessing::MiniMagick::Processor

Inherits:
Processor
  • Object
show all
Defined in:
lib/image_processing/mini_magick.rb

Defined Under Namespace

Modules: Utils

Constant Summary collapse

SHARPEN_PARAMETERS =

Default sharpening parameters used on generated thumbnails.

{ radius: 0, sigma: 1 }

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Processor

accumulator, apply_operation, #apply_operation, call, #custom, #initialize, supports_resize_on_load?

Constructor Details

This class inherits a constructor from ImageProcessing::Processor

Class Method Details

.load_image(path_or_magick, loader: nil, page: nil, geometry: nil, auto_orient: true, **options) ⇒ Object

Initializes the image on disk into a MiniMagick::Tool object. Accepts additional options related to loading the image (e.g. geometry). Additionally auto-orients the image to be upright.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/image_processing/mini_magick.rb', line 36

def self.load_image(path_or_magick, loader: nil, page: nil, geometry: nil, auto_orient: true, **options)
  if path_or_magick.is_a?(::MiniMagick::Tool)
    magick = path_or_magick
  else
    source_path = path_or_magick
    magick = ::ImageProcessing::MiniMagick.convert_shim

    Utils.apply_options(magick, **options)

    input  = source_path
    input  = "#{loader}:#{input}" if loader
    input += "[#{page}]" if page
    input += "[#{geometry}]" if geometry

    magick << input
  end

  magick.auto_orient if auto_orient
  magick
end

.save_image(magick, destination_path, allow_splitting: false, **options) ⇒ Object

Calls the built ImageMagick command to perform processing and save the result to disk. Accepts additional options related to saving the image (e.g. quality).



60
61
62
63
64
65
66
67
# File 'lib/image_processing/mini_magick.rb', line 60

def self.save_image(magick, destination_path, allow_splitting: false, **options)
  Utils.apply_options(magick, **options)

  magick << destination_path
  magick.call

  Utils.disallow_split_layers!(destination_path) unless allow_splitting
end

Instance Method Details

#append(*args) ⇒ Object

Appends a raw ImageMagick command-line argument to the command.



167
168
169
170
171
172
173
# File 'lib/image_processing/mini_magick.rb', line 167

def append(*args)
  if args.empty?
    magick.append
  else
    magick.merge! args
  end
end

#composite(overlay = :none, mask: nil, mode: nil, gravity: nil, offset: nil, args: nil, **options) {|magick| ... } ⇒ Object

Overlays the specified image over the current one. Supports specifying an additional mask, composite mode, direction or offset of the overlay image.

Yields:

  • (magick)


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/image_processing/mini_magick.rb', line 123

def composite(overlay = :none, mask: nil, mode: nil, gravity: nil, offset: nil, args: nil, **options, &block)
  return magick.composite if overlay == :none

  if options.key?(:compose)
    warn "[IMAGE_PROCESSING] The :compose parameter in #composite has been renamed to :mode, the :compose alias will be removed in ImageProcessing 2."
    mode = options[:compose]
  end

  if options.key?(:geometry)
    warn "[IMAGE_PROCESSING] The :geometry parameter in #composite has been deprecated and will be removed in ImageProcessing 2. Use :offset instead, e.g. `geometry: \"+10+15\"` should be replaced with `offset: [10, 15]`."
    geometry = options[:geometry]
  end
  geometry = "%+d%+d" % offset if offset

  overlay_path = convert_to_path(overlay, "overlay")
  mask_path    = convert_to_path(mask, "mask") if mask

  magick << overlay_path
  magick << mask_path if mask_path

  magick.compose(mode) if mode
  define(compose: { args: args }) if args

  magick.gravity(gravity) if gravity
  magick.geometry(geometry) if geometry

  yield magick if block_given?

  magick.composite
end

#crop(*args) ⇒ Object

Crops the image with the specified crop points.



104
105
106
107
108
109
110
# File 'lib/image_processing/mini_magick.rb', line 104

def crop(*args)
  case args.count
  when 1 then magick.crop(*args)
  when 4 then magick.crop("#{args[2]}x#{args[3]}+#{args[0]}+#{args[1]}")
  else fail ArgumentError, "wrong number of arguments (expected 1 or 4, got #{args.count})"
  end
end

#define(options) ⇒ Object

Defines settings from the provided hash.



155
156
157
158
# File 'lib/image_processing/mini_magick.rb', line 155

def define(options)
  return magick.define(options) if options.is_a?(String)
  Utils.apply_define(magick, options)
end

#limits(options) ⇒ Object

Specifies resource limits from the provided hash.



161
162
163
164
# File 'lib/image_processing/mini_magick.rb', line 161

def limits(options)
  options.each { |type, value| magick.args.unshift("-limit", type.to_s, value.to_s) }
  magick
end

#resize_and_pad(width, height, background: :transparent, gravity: "Center", **options) ⇒ Object

Resizes the image to fit within the specified dimensions and fills the remaining area with the specified background color.



90
91
92
93
94
95
# File 'lib/image_processing/mini_magick.rb', line 90

def resize_and_pad(width, height, background: :transparent, gravity: "Center", **options)
  thumbnail("#{width}x#{height}", **options)
  magick.background color(background)
  magick.gravity gravity
  magick.extent "#{width}x#{height}"
end

#resize_to_cover(width, height, **options) ⇒ Object

Resizes the image to cover the specified dimensions, without cropping the excess.



99
100
101
# File 'lib/image_processing/mini_magick.rb', line 99

def resize_to_cover(width, height, **options)
  thumbnail("#{width}x#{height}^", **options)
end

#resize_to_fill(width, height, gravity: "Center", **options) ⇒ Object

Resizes the image to fill the specified dimensions, applying any necessary cropping.



81
82
83
84
85
86
# File 'lib/image_processing/mini_magick.rb', line 81

def resize_to_fill(width, height, gravity: "Center", **options)
  thumbnail("#{width}x#{height}^", **options)
  magick.gravity gravity
  magick.background color(:transparent)
  magick.extent "#{width}x#{height}"
end

#resize_to_fit(width, height, **options) ⇒ Object

Resizes the image to fit within the specified dimensions.



75
76
77
# File 'lib/image_processing/mini_magick.rb', line 75

def resize_to_fit(width, height, **options)
  thumbnail("#{width}x#{height}", **options)
end

#resize_to_limit(width, height, **options) ⇒ Object

Resizes the image to not be larger than the specified dimensions.



70
71
72
# File 'lib/image_processing/mini_magick.rb', line 70

def resize_to_limit(width, height, **options)
  thumbnail("#{width}x#{height}>", **options)
end

#rotate(degrees, background: nil) ⇒ Object

Rotates the image by an arbitrary angle. For angles that are not multiple of 90 degrees an optional background color can be specified to fill in the gaps.



115
116
117
118
# File 'lib/image_processing/mini_magick.rb', line 115

def rotate(degrees, background: nil)
  magick.background color(background) if background
  magick.rotate(degrees)
end