class ImageProcessing::MiniMagick::Processor
def self.load_image(path_or_magick, loader: nil, page: nil, geometry: nil, auto_orient: true, **options)
additional options related to loading the image (e.g. geometry).
Initializes the image on disk into a MiniMagick::Tool object. Accepts
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
def self.save_image(magick, destination_path, allow_splitting: false, **options)
the result to disk. Accepts additional options related to saving the
Calls the built ImageMagick command to perform processing and save
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
def append(*args)
def append(*args) if args.empty? magick.append else magick.merge! args end end
def color(value)
This supports specifying RGB(A) values with arrays, which mainly exists
Converts the given color value into an identifier ImageMagick understands.
def color(value) return "rgba(255,255,255,0.0)" if value.to_s == "transparent" return "rgb(#{value.join(",")})" if value.is_a?(Array) && value.count == 3 return "rgba(#{value.join(",")})" if value.is_a?(Array) && value.count == 4 return value if value.is_a?(String) raise ArgumentError, "unrecognized color format: #{value.inspect} (must be one of: string, 3-element RGB array, 4-element RGBA array)" end
def composite(overlay = :none, mask: nil, mode: nil, gravity: nil, offset: nil, args: nil, **options, &block)
an additional mask, composite mode, direction or offset of the overlay
Overlays the specified image over the current one. Supports specifying
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
def convert_to_path(file, name)
def convert_to_path(file, name) if file.is_a?(String) file elsif file.respond_to?(:to_path) file.to_path elsif file.respond_to?(:path) file.path else raise ArgumentError, "#{name} must be a String, Pathname, or respond to #path" end end
def crop(*args)
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
def define(options)
def define(options) return magick.define(options) if options.is_a?(String) Utils.apply_define(magick, options) end
def limits(options)
def limits(options) options.each { |type, value| magick.args.unshift("-limit", type.to_s, value.to_s) } magick end
def resize_and_pad(width, height, background: :transparent, gravity: "Center", **options)
Resizes the image to fit within the specified dimensions and fills
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
def resize_to_cover(width, height, **options)
Resizes the image to cover the specified dimensions, without
def resize_to_cover(width, height, **options) thumbnail("#{width}x#{height}^", **options) end
def resize_to_fill(width, height, gravity: "Center", **options)
Resizes the image to fill the specified dimensions, applying any
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
def resize_to_fit(width, height, **options)
def resize_to_fit(width, height, **options) thumbnail("#{width}x#{height}", **options) end
def resize_to_limit(width, height, **options)
def resize_to_limit(width, height, **options) thumbnail("#{width}x#{height}>", **options) end
def rotate(degrees, background: nil)
multiple of 90 degrees an optional background color can be specified to
Rotates the image by an arbitrary angle. For angles that are not
def rotate(degrees, background: nil) magick.background color(background) if background magick.rotate(degrees) end
def thumbnail(geometry, sharpen: nil)
Resizes the image using the specified geometry, and sharpens the
def thumbnail(geometry, sharpen: nil) magick.resize(geometry) if sharpen sharpen = SHARPEN_PARAMETERS.merge(sharpen) magick.sharpen("#{sharpen[:radius]}x#{sharpen[:sigma]}") end magick end