Skip to content

Instantly share code, notes, and snippets.

@swanandp
Created January 13, 2012 18:10
Show Gist options
  • Save swanandp/1607836 to your computer and use it in GitHub Desktop.
Save swanandp/1607836 to your computer and use it in GitHub Desktop.
An ugly extension to add text underneath the barcode image generated by Barby
module Barby
class SwanandRmagickOutputter < RmagickOutputter
register :to_jpg_2
def to_jpg_2(*a)
to_blob('jpg', *a)
end
#Returns a string containing a JPEG image
def to_blob(format, *a)
img = to_image(*a)
add_text(img)
blob = img.to_blob{|i| i.format = format }
#Release the memory used by RMagick explicitly. Ruby's GC
#isn't aware of it and can't clean it up automatically
img.destroy! if img.respond_to?(:destroy!)
blob
end
def add_text(canvas)
text = Magick::Draw.new
text.font_family = 'Courier'
text.pointsize = 14
text.gravity = Magick::SouthGravity
text.undercolor = 'white'
text.font_stretch = Magick::ExtraExpandedStretch
text.annotate(canvas, 0, 0, 0, 8, " #{barcode.data} ")
canvas
end
end
end
# I put this in my controllers
require 'barby/barcode/code_39'
require 'barby/outputter/rmagick_outputter'
require 'lib/swanand_rmagick_outputter'
def barcode_test
barcode = Barby::Code39.new(@key, true)
send_data(barcode.to_jpg_2(:height => 60),
:type => 'image/jpeg',
:disposition => 'inline'
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment