Class: Quirc::ImageProcessor::MiniMagick

Inherits:
Quirc::ImageProcessor show all
Defined in:
lib/quirc/image_processor/mini_magick.rb

Instance Attribute Summary

Attributes inherited from Quirc::ImageProcessor

#pathname

Instance Method Summary collapse

Methods inherited from Quirc::ImageProcessor

#initialize

Constructor Details

This class inherits a constructor from Quirc::ImageProcessor

Instance Method Details

#to_grayscale(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/quirc/image_processor/mini_magick.rb', line 5

def to_grayscale(*args)
  read_image do |image|
    with_tempfile("-grayscale-8bit.tiff") do |tempfile|  
      options = args.last.is_a?(Hash) ? args.pop : {}
      width, height = args
      width  ||= options.delete(:width) || image.width
      height ||= options.delete(:height) || image.height

      pipeline = ImageProcessing::MiniMagick.source(image)
      if width != image.width || height != image.height
        pipeline = pipeline.resize_to_fit(width, height, **options)
      end
      pipeline  = pipeline.depth(8)
      pipeline  = pipeline.colorspace("Gray")
      pipeline  = pipeline.alpha("remove")
      pipeline  = pipeline.convert("tiff")
      pipeline.call(destination: tempfile.path)
      processed = ::MiniMagick::Image.new(tempfile.path)

      tempfile.rewind
      yield tempfile.read, processed.width, processed.height
    end
  end
end