From ae5d464f89f4a161678e1a6ba668f1aac13033bf Mon Sep 17 00:00:00 2001 From: shields Date: Sun, 19 Sep 2021 00:05:54 +0900 Subject: [PATCH 1/4] - Support default processor using either MiniMagick or ChunkyPNG (whichever one is available). Default is MiniMagick if both are installed. - :processor arg is now more permissive (allows symbol or string, case-insensitive and underscore/hyphen-insensitive) - Remove usage of const_get --- lib/escpos/image.rb | 26 +++++++++++++++++++++++--- test/lib/escpos/image_test.rb | 6 ++++++ test/test_helper.rb | 3 +++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/escpos/image.rb b/lib/escpos/image.rb index 61d8e1e..100f16c 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(options[:processor]) + @processor = processor_klass.new(image_or_path, options) @processor.process! end @@ -53,5 +52,26 @@ def to_escpos ].join end + private + + def get_processor_klass(processor) + 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 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..4c1243c 100644 --- a/test/lib/escpos/image_test.rb +++ b/test/lib/escpos/image_test.rb @@ -40,4 +40,10 @@ def test_image_conversion assert_equal IO.binread(file), @printer.to_escpos end + def test_default_processor_klass + 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__) From 09566cf9c3828260fea6ee443b5cb6a1e2fa7a6d Mon Sep 17 00:00:00 2001 From: shields Date: Sun, 19 Sep 2021 00:15:40 +0900 Subject: [PATCH 2/4] Update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 } From 09cb632860f81a1a6b1be5af758d75085f489a92 Mon Sep 17 00:00:00 2001 From: shields Date: Sun, 19 Sep 2021 00:24:59 +0900 Subject: [PATCH 3/4] Load processor if image is explicitly loaded --- lib/escpos/image.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/escpos/image.rb b/lib/escpos/image.rb index 100f16c..456dc6e 100644 --- a/lib/escpos/image.rb +++ b/lib/escpos/image.rb @@ -16,7 +16,7 @@ class Image def initialize(image_or_path, options = {}) @options = options - processor_klass = get_processor_klass(options[:processor]) + processor_klass = get_processor_klass(image_or_path, options[:processor]) @processor = processor_klass.new(image_or_path, options) @processor.process! @@ -54,7 +54,12 @@ def to_escpos private - def get_processor_klass(processor) + def get_processor_klass(image, processor) + case image + when ::MiniMagick::Image then return ImageProcessors::MiniMagick + when ::ChunkyPng::Image then return ImageProcessors::ChunkyPng + end + return default_processor_klass unless processor case processor.to_s.downcase.gsub(/[_-]/, '') From c659e527c2d355a6165c6b3074febf7935f58915 Mon Sep 17 00:00:00 2001 From: shields Date: Sun, 19 Sep 2021 00:53:12 +0900 Subject: [PATCH 4/4] Fix loading from image --- lib/escpos/image.rb | 13 +++++++++---- test/lib/escpos/image_test.rb | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/escpos/image.rb b/lib/escpos/image.rb index 456dc6e..6cd68d1 100644 --- a/lib/escpos/image.rb +++ b/lib/escpos/image.rb @@ -55,10 +55,8 @@ def to_escpos private def get_processor_klass(image, processor) - case image - when ::MiniMagick::Image then return ImageProcessors::MiniMagick - when ::ChunkyPng::Image then return ImageProcessors::ChunkyPng - end + klass = get_processor_klass_from_image(image) + return klass if klass return default_processor_klass unless processor @@ -69,6 +67,13 @@ def get_processor_klass(image, processor) 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 diff --git a/test/lib/escpos/image_test.rb b/test/lib/escpos/image_test.rb index 4c1243c..8d3003f 100644 --- a/test/lib/escpos/image_test.rb +++ b/test/lib/escpos/image_test.rb @@ -40,7 +40,20 @@ def test_image_conversion assert_equal IO.binread(file), @printer.to_escpos end - def test_default_processor_klass + 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