Module: ImageProcessing::MiniMagick::Processor::Utils

Defined in:
lib/image_processing/mini_magick.rb

Class Method Summary collapse

Class Method Details

.apply_define(magick, options) ⇒ Object

Applies settings from the provided (nested) hash.



246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/image_processing/mini_magick.rb', line 246

def apply_define(magick, options)
  options.each do |namespace, settings|
    namespace = namespace.to_s.tr("_", "-")

    settings.each do |key, value|
      key = key.to_s.tr("_", "-")

      magick.define "#{namespace}:#{key}=#{value}"
    end
  end

  magick
end

.apply_options(magick, define: {}, **options) ⇒ Object

Applies options from the provided hash.



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/image_processing/mini_magick.rb', line 226

def apply_options(magick, define: {}, **options)
  options.each do |option, value|
    # Loader/saver option names may come from user input, so guard
    # against dispatching to unsafe Ruby core methods (e.g. #send,
    # #instance_eval) that public_send does not block on its own.
    if ImageProcessing.unsafe_method?(magick, option)
      raise Error, "#{option.inspect} is not a valid option"
    end

    case value
    when true, nil then magick.public_send(option)
    when false     then magick.public_send(option).+
    else                magick.public_send(option, *value)
    end
  end

  apply_define(magick, define)
end

.disallow_split_layers!(destination_path) ⇒ Object

When a multi-layer format is being converted into a single-layer format, ImageMagick will create multiple images, one for each layer. We want to warn the user that this is probably not what they wanted.



216
217
218
219
220
221
222
223
# File 'lib/image_processing/mini_magick.rb', line 216

def disallow_split_layers!(destination_path)
  layers = Dir[destination_path.sub(/(\.\w+)?$/, '-*\0')]

  if layers.any?
    layers.each { |path| File.delete(path) }
    raise Error, "Source format is multi-layer, but destination format is single-layer. If you care only about the first layer, add `.loader(page: 0)` to your pipeline. If you want to process each layer, see https://github.com/janko/image_processing/wiki/Splitting-a-PDF-into-multiple-images or use `.saver(allow_splitting: true)`."
  end
end