Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
36 changes: 33 additions & 3 deletions lib/escpos/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
19 changes: 19 additions & 0 deletions test/lib/escpos/image_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
require 'pp'

require 'escpos'
require 'chunky_png'
require 'mini_magick'

require File.expand_path('../../lib/escpos/image.rb', __FILE__)