class ImageProcessing::MiniMagick::Processor
Constants
- SHARPEN_PARAMETERS
-
Default sharpening parameters used on generated thumbnails.
Public Class Methods
Source
# 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
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.
Source
# 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
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).
Public Instance Methods
Source
# File lib/image_processing/mini_magick.rb, line 167 def append(*args) if args.empty? magick.append else magick.merge! args end end
Appends a raw ImageMagick command-line argument to the command.
Source
# 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
Overlays the specified image over the current one. Supports specifying an additional mask, composite mode, direction or offset of the overlay image.
Source
# 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
Crops the image with the specified crop points.
Source
# 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
Defines settings from the provided hash.
Source
# 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
Specifies resource limits from the provided hash.
Source
# 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
Resizes the image to fit within the specified dimensions and fills the remaining area with the specified background color.
Source
# File lib/image_processing/mini_magick.rb, line 99 def resize_to_cover(width, height, **options) thumbnail("#{width}x#{height}^", **options) end
Resizes the image to cover the specified dimensions, without cropping the excess.
Source
# 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
Resizes the image to fill the specified dimensions, applying any necessary cropping.
Source
# File lib/image_processing/mini_magick.rb, line 75 def resize_to_fit(width, height, **options) thumbnail("#{width}x#{height}", **options) end
Resizes the image to fit within the specified dimensions.
Source
# File lib/image_processing/mini_magick.rb, line 70 def resize_to_limit(width, height, **options) thumbnail("#{width}x#{height}>", **options) end
Resizes the image to not be larger than the specified dimensions.
Source
# File lib/image_processing/mini_magick.rb, line 115 def rotate(degrees, background: nil) magick.background color(background) if background magick.rotate(degrees) 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.
Private Instance Methods
Source
# File lib/image_processing/mini_magick.rb, line 180 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 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.
Source
# File lib/image_processing/mini_magick.rb, line 203 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
Converts the image on disk in various forms into a path.
Source
# File lib/image_processing/mini_magick.rb, line 191 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
Resizes the image using the specified geometry, and sharpens the resulting thumbnail.