Module: Converter

Extended by:
Gbarcode
Includes:
Gbarcode
Defined in:
lib/barcode_service.rb

Class Method Summary collapse

Class Method Details

.encode(code, format, options) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/barcode_service.rb', line 74

def self.encode(code, format, options)
  require 'stringio'

  barcode = barcode_create(code)
  barcode.width   = options['width'].to_i
  barcode.height  = options['height'].to_i
  barcode.xoff    = options['x'].to_i
  barcode.yoff    = options['y'].to_i
  barcode.scalef  = options['scale'].to_i
  barcode_encode barcode, BARCODE_NO_CHECKSUM | const_get(TYPES[options['type']])

  file = Tempfile.new('barcode')
  File.open(file.path, 'w') do |f|
    barcode_print(barcode, f, BARCODE_OUT_EPS)
  end

  output = case format
  when 'eps'
    file.read
  when "png"
    %x{convert eps:#{file.path} png:-}
  when "jpg", "jpeg"
    %x{convert eps:#{file.path} jpg:-}
  when "gif"
    %x{convert eps:#{file.path} gif:-}
  end

  file.unlink

  output
end