class ImageProcessing::MiniMagick::Processor
Constants
- SHARPEN_PARAMETERS
Default sharpening parameters used on generated thumbnails.
Public Class Methods
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.
# File lib/image_processing/mini_magick.rb, line 28 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 = ::MiniMagick::Tool::Convert.new 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
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).
# File lib/image_processing/mini_magick.rb, line 52 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
Public Instance Methods
Appends a raw ImageMagick command-line argument to the command.
# File lib/image_processing/mini_magick.rb, line 153 def append(*args) magick.merge! args end
Overlays the specified image over the current one. Supports specifying an additional mask, composite mode, direction or offset of the overlay image.
# File lib/image_processing/mini_magick.rb, line 109 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
Crops the image with the specified crop points.
# File lib/image_processing/mini_magick.rb, line 90 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
Defines settings from the provided hash.
# File lib/image_processing/mini_magick.rb, line 141 def define(options) return magick.define(options) if options.is_a?(String) Utils.apply_define(magick, options) end
Specifies resource limits from the provided hash.
# File lib/image_processing/mini_magick.rb, line 147 def limits(options) options.each { |type, value| magick.args.unshift("-limit", type.to_s, value.to_s) } magick end
Resizes the image to fit within the specified dimensions and fills the remaining area with the specified background color.
# File lib/image_processing/mini_magick.rb, line 82 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
Resizes the image to fill the specified dimensions, applying any necessary cropping.
# File lib/image_processing/mini_magick.rb, line 73 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
Resizes the image to fit within the specified dimensions.
# File lib/image_processing/mini_magick.rb, line 67 def resize_to_fit(width, height, **options) thumbnail("#{width}x#{height}", **options) end
Resizes the image to not be larger than the specified dimensions.
# File lib/image_processing/mini_magick.rb, line 62 def resize_to_limit(width, height, **options) thumbnail("#{width}x#{height}>", **options) end
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.
# File lib/image_processing/mini_magick.rb, line 101 def rotate(degrees, background: nil) magick.background color(background) if background magick.rotate(degrees) end
Private Instance Methods
Converts the given color value into an identifier ImageMagick understands. This supports specifying RGB(A) values with arrays, which mainly exists for compatibility with the libvips implementation.
# File lib/image_processing/mini_magick.rb, line 162 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
Converts the image on disk in various forms into a path.
# File lib/image_processing/mini_magick.rb, line 185 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
Resizes the image using the specified geometry, and sharpens the resulting thumbnail.
# File lib/image_processing/mini_magick.rb, line 173 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