Module: ZplRender::Output::Png
- Defined in:
- lib/zpl_render/output/png.rb
Class Method Summary collapse
-
.encode(canvas, scale: 1) ⇒ Object
Encode a Canvas as a PNG binary string.
Class Method Details
.encode(canvas, scale: 1) ⇒ Object
Encode a Canvas as a PNG binary string. scale is an integer
nearest-neighbor upscale (keeps barcode modules pixel-exact).
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/zpl_render/output/png.rb', line 12 def encode(canvas, scale: 1) scale = [scale.to_i, 1].max image = ChunkyPNG::Image.new(canvas.width * scale, canvas.height * scale, ChunkyPNG::Color::WHITE) black = ChunkyPNG::Color::BLACK (0...canvas.height).each do |y| (0...canvas.width).each do |x| next unless canvas.get(x, y) if scale == 1 image[x, y] = black else (0...scale).each do |dy| (0...scale).each do |dx| image[x * scale + dx, y * scale + dy] = black end end end end end image.to_blob(:fast_rgb) end |