diff --git a/README.md b/README.md index a1deece..19b63e1 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ or # Creating image from path image = Escpos::Image.new 'path/to/image.png', { - processor: "ChunkyPng" # or MiniMagick + processor: :chunky_png # or :mini_magick # ... other options, see following sections } diff --git a/lib/escpos/image.rb b/lib/escpos/image.rb index 61d8e1e..6cd68d1 100644 --- a/lib/escpos/image.rb +++ b/lib/escpos/image.rb @@ -16,9 +16,8 @@ class Image def initialize(image_or_path, options = {}) @options = options - processor_klass_name = options.fetch(:processor) - processor_klass = ImageProcessors.const_get(processor_klass_name) - @processor = processor_klass.new image_or_path, options + processor_klass = get_processor_klass(image_or_path, options[:processor]) + @processor = processor_klass.new(image_or_path, options) @processor.process! end @@ -53,5 +52,36 @@ def to_escpos ].join end + private + + def get_processor_klass(image, processor) + klass = get_processor_klass_from_image(image) + return klass if klass + + return default_processor_klass unless processor + + case processor.to_s.downcase.gsub(/[_-]/, '') + when 'minimagick' then ImageProcessors::MiniMagick + when 'chunkypng' then ImageProcessors::ChunkyPng + else raise("Escpos:Image unknown processor value #{processor.inspect}") + end + end + + def get_processor_klass_from_image(image) + case image + when ::MiniMagick::Image then ImageProcessors::MiniMagick + when ::ChunkyPNG::Image then ImageProcessors::ChunkyPng + end + end + + def default_processor_klass + if defined?(MiniMagick) + ImageProcessors::MiniMagick + elsif defined?(ChunkyPNG) + ImageProcessors::ChunkyPng + else + raise('Escpos:Image requires either mini_magick or chunky_png gem.') + end + end end end diff --git a/test/lib/escpos/image_test.rb b/test/lib/escpos/image_test.rb index 1bf075e..8d3003f 100644 --- a/test/lib/escpos/image_test.rb +++ b/test/lib/escpos/image_test.rb @@ -40,4 +40,23 @@ def test_image_conversion assert_equal IO.binread(file), @printer.to_escpos end + def test_processor_chunky_png_image + image = Escpos::Image.new ChunkyPNG::Image.new(8, 8), grayscale: true, + compose_alpha: true, extent: true + assert_equal image.processor.class, Escpos::ImageProcessors::ChunkyPng + end + + def test_processor_mini_magick_image + image_path = File.join(__dir__, '../../fixtures/tux_mono.png') + image = Escpos::Image.new MiniMagick::Image.new(image_path), grayscale: true, + compose_alpha: true, extent: true + assert_equal image.processor.class, Escpos::ImageProcessors::MiniMagick + end + + def test_processor_default + image_path = File.join(__dir__, '../../fixtures/tux_alpha.png') + image = Escpos::Image.new image_path, grayscale: true, + compose_alpha: true, extent: true + assert_equal image.processor.class, Escpos::ImageProcessors::MiniMagick + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 812c543..6082943 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,4 +5,7 @@ require 'pp' require 'escpos' +require 'chunky_png' +require 'mini_magick' + require File.expand_path('../../lib/escpos/image.rb', __FILE__)