module MojoMagick

Option builder used in convert and mogrify helpers.

rubocop:disable Lint/AssignmentInCondition

Constants

CommandStatus
VERSION

Public Class Methods

available_fonts() click to toggle source
# File lib/mojo_magick.rb, line 148
def self.available_fonts
  # returns width, height of image if available, nil if not
  Font.all
end
convert(source = nil, dest = nil) { |opts| ... } click to toggle source
# File lib/mojo_magick.rb, line 132
def self.convert(source = nil, dest = nil)
  opts = OptBuilder.new
  opts.file source if source
  yield opts
  opts.file dest if dest

  Commands.raw_command("convert", *opts.to_a)
end
expand(source_file, dest_file, options) click to toggle source
# File lib/mojo_magick.rb, line 103
def self.expand(source_file, dest_file, options)
  opts = options.dup
  opts.delete(:shrink_only)
  MojoMagick.resize(source_file, dest_file, opts.merge(expand_only: true))
end
get_format(source_file, format_string) click to toggle source
# File lib/mojo_magick.rb, line 153
def self.get_format(source_file, format_string)
  Commands.raw_command("identify", "-format", format_string, source_file)
end
get_image_size(source_file) click to toggle source

returns an empty hash or a hash with :width and :height set (e.g. {:width => INT, :height => INT}) raises MojoFailed when results are indeterminate (width and height could not be determined)

# File lib/mojo_magick.rb, line 159
def self.get_image_size(source_file)
  # returns width, height of image if available, nil if not
  retval = get_format(source_file, "w:%w h:%h")
  return {} unless retval

  width = retval.match(/w:([0-9]+) /)
  width = width ? width[1].to_i : nil
  height = retval.match(/h:([0-9]+)/)
  height = height ? height[1].to_i : nil
  raise(MojoFailed, "Indeterminate results in get_image_size: #{source_file}") if !height || !width

  { width: width, height: height }
end
mogrify(dest = nil) { |opts| ... } click to toggle source
# File lib/mojo_magick.rb, line 141
def self.mogrify(dest = nil)
  opts = OptBuilder.new
  yield opts
  opts.file dest if dest
  Commands.raw_command("mogrify", *opts.to_a)
end
resize(source_file, dest_file, options) click to toggle source

resizes an image and returns the filename written to options:

:width / :height => scale to these dimensions
:scale => pass scale options such as ">" to force shrink scaling only or
          "!" to force absolute width/height scaling (do not preserve aspect ratio)
:percent => scale image to this percentage (do not specify :width/:height in this case)
# File lib/mojo_magick.rb, line 115
def self.resize(source_file, dest_file, options)
  scale_options = extract_scale_options(options)
  geometry = extract_geometry_options(options)
  extras = []
  if !options[:fill].nil? && !options[:crop].nil?
    extras << "-gravity"
    extras << "Center"
    extras << "-extent"
    extras << geometry.to_s
  end
  Commands.raw_command("convert",
                       source_file,
                       "-resize", "#{geometry}#{scale_options}",
                       *extras, dest_file)
  dest_file
end
shrink(source_file, dest_file, options) click to toggle source
# File lib/mojo_magick.rb, line 97
def self.shrink(source_file, dest_file, options)
  opts = options.dup
  opts.delete(:expand_only)
  MojoMagick.resize(source_file, dest_file, opts.merge(shrink_only: true))
end
tempfile(*opts) click to toggle source
# File lib/mojo_magick.rb, line 173
def self.tempfile(*opts)
  data = opts[0]
  rest = opts[1]
  ext = rest && rest[:format]
  file = Tempfile.new(["mojo", ext ? ".#{ext}" : ""])
  file.binmode
  file.write(data)
  file.path
ensure
  file.close
end
windows?() click to toggle source
# File lib/mojo_magick.rb, line 93
def self.windows?
  !RUBY_PLATFORM.include(win32)
end

Private Class Methods

extract_geometry_options(options) click to toggle source
# File lib/mojo_magick.rb, line 188
def extract_geometry_options(options)
  if !options[:width].nil? && !options[:height].nil?
    "#{options[:width]}X#{options[:height]}"
  elsif !options[:percent].nil?
    "#{options[:percent]}%"
  else
    raise MojoMagickError, "Resize requires width and height or percentage: #{options.inspect}"
  end
end
extract_scale_options(options) click to toggle source
# File lib/mojo_magick.rb, line 198
def extract_scale_options(options)
  [].tap { |scale_options|
    scale_options << ">" unless options[:shrink_only].nil?
    scale_options << "<" unless options[:expand_only].nil?
    scale_options << "!" unless options[:absolute_aspect].nil?
    scale_options << "^" unless options[:fill].nil?
  }.join
end