rotation patch #2

Open
donpdonp opened this Issue May 20, 2011 · 0 comments

Projects

None yet

1 participant

@donpdonp

Hi. We needed image rotation, and an html id other than 'barcode' so I thought Id send a patch your way. thanks!

diff --git a/README.markdown b/README.markdown
index a967b2d..b866c43 100644
--- a/README.markdown
+++ b/README.markdown
@@ -40,6 +40,9 @@ To customize your barcodes, you can optionally pass following information in you

  • margin
  • no_ascii (accepts boolean true or false, prevents the ascii string from printing at the bottom of the barcode)
  • output_type (accepts :disk or :stream. :disk will print the image to disk, and serve it regularly.)

    • + rotate (degrees of rotation from -180 to 180)
    • + html_id (for the img tag)
    • + html_class (for the img tag)

    in this case your view will look like:

diff --git a/lib/barcoder.rb b/lib/barcoder.rb
index 7b7b074..2e4351c 100644
--- a/lib/barcoder.rb
+++ b/lib/barcoder.rb
@@ -12,7 +12,7 @@ module ActionView
class Base

 # important defaults, should not be messed with.
  • VALID_BARCODER_OPTIONS = [:encoding_format, :output_format, :width, :height, :scaling_factor, :xoff, :yoff, :margin, :output_type]
  • VALID_BARCODER_OPTIONS = [:encoding_format, :output_format, :width, :height, :scaling_factor, :xoff, :yoff, :margin, :output_type, :rotate, :html_id, :html_class]
    DEFAULT_BARCODER_OUTPUT_FORMAT = 'gif'
    DEFAULT_BARCODER_ENCODING = Gbarcode::BARCODE_39 | Gbarcode::BARCODE_NO_CHECKSUM
    BARCODE_STORAGE_PATH = "public/images/barcodes"
    @@ -42,12 +42,13 @@ module ActionView
    print_options = Gbarcode::BARCODE_OUT_EPS
    end

  •  rotate_opt = "-rotate #{options[:rotate]}" if options[:rotate]
    

    # this is where the magic happens.

  • data = echo "#{get_bytes_from_barcode(bc, print_options)}" | convert eps: #{output_format}:
  •  data = `echo "#{get_bytes_from_barcode(bc, print_options)}" | convert #{rotate_opt} eps: #{output_format}:`
    

    # simple output strategy, define :output_type => :disk in the #to_barcode call if you want
    # it to write out to the disk for you, otherwise it will be a data url stream.

  • output_type == :disk ? barcode_to_disk(data, bc, output_format) : barcode_to_stream(data, output_format, str)
  •  output_type == :disk ? barcode_to_disk(data, bc, output_format) : barcode_to_stream(data, output_format, str, options[:html_id], options[:html_class])
    

    end

    # support for the original barcode-generator plugin syntax.
    @@ -59,21 +60,21 @@ module ActionView

    # stream the barcode to disk. this may be necessary for some cases, but if you
    # are living on a cluster node like say, heroku, this won't work out well for you.

  • def barcode_to_disk(data, barcode, output_format)
  • def barcode_to_disk(data, barcode, output_format, html_id="barcode", html_class="barcode") filename = "#{barcode.ascii.gsub(" ", "-")}.#{output_format}" Dir.mkdir(BARCODE_STORAGE_PATH) unless File.directory?(BARCODE_STORAGE_PATH) File.open("#{BARCODE_STORAGE_PATH}/#{filename}", 'w') do |f| f.write(data) end
  • image_tag("barcodes/#{filename}", :id => "barcode", :class => "barcode")
  •  image_tag("barcodes/#{filename}", :id => "#{html_id}", :class => "#{html_class}")
    

    end

    # stream the barcode to the client as a data url. often times, the barcode
    # filesize is so minute, that this is absolutely acceptable. NOTE: I intentionally
    # draw my own img tag for this, image_tag doesn't really like this.

  • def barcode_to_stream(data, format, str)
  • def barcode_to_stream(data, format, str, html_id="barcode", html_class="barcode") src = "data:image/#{format};base64,#{Base64.encode64(data)}"
  • %Q{#{str}}
  •  %Q{<img src="#{src}" alt="#{str}" id="#{html_id}" class="#{html_class}" />}
    

    end

    # this method tricks GBarcode into printing the contents of the EPS into
    @@ -88,4 +89,4 @@ module ActionView
    end

    end
    -end
    \ No newline at end of file
    +end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment