Skip to content

Instantly share code, notes, and snippets.

@lazyatom
Created April 3, 2012 11:06
Show Gist options
  • Save lazyatom/2291071 to your computer and use it in GitHub Desktop.
Save lazyatom/2291071 to your computer and use it in GitHub Desktop.
Test printer code
require "rubygems"
require "bundler/setup"
require "sinatra"
require "base64"
get "/" do
Base64.encode64(File.binread("image"))
end
require "rubygems"
require "bundler/setup"
require "sinatra"
require "resque"
$LOAD_PATH.unshift "."
require "image"
get "/" do
queued_image = Resque.reserve(Image.queue)
if queued_image
queued_image.payload["args"]
end
end
require "rubygems"
require "bundler/setup"
require "base64"
require "RMagick"
require "resque"
require 'rvg/rvg'
include Magick
require "image"
def full_width_image(&block)
RVG::dpi = 100
RVG.new(3.84.in, 3.84.in).viewbox(0,0,384,384, &block)
end
def rvg
x = full_width_image do |c|
c.background_fill = 'white'
c.use(gfr_logo)
c.text(380, 380, Time.now.to_s).styles(text_anchor: "end", font_family: "foco", font_size: 18)
end
flip(x)
end
def flip(img)
img.rotate(180).translate(-384, -384)
end
def gfr_logo
RVG::Group.new do |c|
c.g.translate(215, 142) do |body|
body.ellipse(140, 140).styles(fill: 'white', stroke: 'black', stroke_width: 3)
body.text(10, 30) do |title|
title.tspan("free").styles(font_size: 120, text_anchor: 'middle',
font_family: 'foco', font_style: 'italic', fill: 'black')
end
body.text(50, 65) do |title|
title.tspan("RANGE").styles(font_size: 40, text_anchor: 'middle',
font_family: 'foco', font_style: 'italic', fill: 'black')
end
end
c.g.translate(57, 120) do |body|
body.ellipse(55, 55).styles(fill: 'white', stroke: 'black', stroke_width: 3)
body.text(0, 18) do |title|
title.tspan("Go").styles(font_size: 52, text_anchor: 'middle',
font_family: 'foco', font_style: 'italic', fill: 'black')
end
end
end
end
def base64_image
img = rvg.draw
img.write("tmp.png")
bits = []
width = img.columns
height = img.rows
img.each_pixel { |pixel, _, _| bits << (pixel.red > 0 ? 0 : 1) }
bytes = []; bits.each_slice(8) { |s| bytes << ("0" + s.join).to_i(2) }
str = [width,height].pack("SS")
str += bytes.pack("C*")
File.open("tmp", "w") { |f| f.write str }
Base64.encode64(str)
end
20.times do
Resque.enqueue(Image, base64_image)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment